Please help by scripting - I am new to python

General discussion about TexGen.

Moderators: Martin, Developers

Post Reply
Henrik.Herranen
Posts: 4
Joined: Mon May 20, 2013 11:48 am

Please help by scripting - I am new to python

Post by Henrik.Herranen »

Hello TexGen enthusiast!

I am developing layered structure, which has foreign object embedded in the structure. Right now I am stuck with the definition of single yarn with hybrid section. How can I get the Yarn[23] section defined. The script is as follows:

# Script recorded by TexGen v3.5.3

# Create a textile
Textile = CTextile()

# Create a python list containing 24 yarns
Yarns = [CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(),CYarn(), CYarn(), CYarn()]

# Add nodes to the yarns to describe their paths
Yarns[0].AddNode(CNode(XYZ(0,0,-1.35690511431748)))
Yarns[0].AddNode(CNode(XYZ(26.1178751920483,0,-1.10333877091771)))

Yarns[1].AddNode(CNode(XYZ(0,0,2.08283416805388)))
Yarns[1].AddNode(CNode(XYZ(7.60110970447422,0,1.75405771805033)))
Yarns[1].AddNode(CNode(XYZ(10.1506477036487,0,1.40667724159241)))
Yarns[1].AddNode(CNode(XYZ(12.7847208471295,0,0.651506576910888)))
Yarns[1].AddNode(CNode(XYZ(15.3727175975557,0,0.04466557866232)))
Yarns[1].AddNode(CNode(XYZ(18.0076219936037,0,-0.178772837708289)))
Yarns[1].AddNode(CNode(XYZ(26.1178751920483,0,-0.509198759375013)))


Yarns[2].AddNode(CNode(XYZ(0,0,2.43083185473508)))
Yarns[2].AddNode(CNode(XYZ(6.23943215714167,0,2.20845136139976)))
Yarns[2].AddNode(CNode(XYZ(9.47454545880161,0,1.80281616563909)))
Yarns[2].AddNode(CNode(XYZ(12.4162965575901,0,1.24534594008006)))
Yarns[2].AddNode(CNode(XYZ(15.7938023300884,0,0.613018734008396)))
Yarns[2].AddNode(CNode(XYZ(19.5004077270064,0,0.13923229264907)))
Yarns[2].AddNode(CNode(XYZ(26.0602739851117,0,-0.121353327941076)))

Yarns[3].AddNode(CNode(XYZ(0,0,3.02932459594996)))
Yarns[3].AddNode(CNode(XYZ(5.65085505200628,0,2.85047827962796)))
Yarns[3].AddNode(CNode(XYZ(10.0586257585936,0,2.26189529090206)))
Yarns[3].AddNode(CNode(XYZ(13.4920265998418,0,1.53749752275292)))
Yarns[3].AddNode(CNode(XYZ(16.7971553969722,0,0.981707961404881)))
Yarns[3].AddNode(CNode(XYZ(20.5534551121967,0,0.566587549982634)))
Yarns[3].AddNode(CNode(XYZ(26.0154469168219,0,0.149824303596482)))

Yarns[4].AddNode(CNode(XYZ(13.0541703920201,12.5,0.717104011532574)))
Yarns[4].AddNode(CNode(XYZ(13.0541703920201,0,0.717104011532574)))
Yarns[5].AddNode(CNode(XYZ(13.5949584821026,0,-0.120682621971694)))
Yarns[5].AddNode(CNode(XYZ(13.5949584821026,12.5,-0.120682621971694)))
Yarns[6].AddNode(CNode(XYZ(13.0541703920201,0,0.717104011532574)))
Yarns[6].AddNode(CNode(XYZ(13.0541703920201,12.5,0.717104011532574)))
Yarns[7].AddNode(CNode(XYZ(10.6734244282232,0,-0.82239746086376)))
Yarns[7].AddNode(CNode(XYZ(10.6734244282232,12.5,-0.82239746086376)))
Yarns[8].AddNode(CNode(XYZ(13.5949584821026,0,-0.120682621971694)))
Yarns[8].AddNode(CNode(XYZ(13.5949584821026,12.5,-0.120682621971694)))
Yarns[9].AddNode(CNode(XYZ(13.5949584821026,0,-0.813927217948378)))
Yarns[9].AddNode(CNode(XYZ(13.5949584821026,12.5,-0.813927217948378)))
Yarns[10].AddNode(CNode(XYZ(10.6734244282232,0,-0.82239746086376)))
Yarns[10].AddNode(CNode(XYZ(10.6734244282232,12.5,-0.82239746086376)))
Yarns[11].AddNode(CNode(XYZ(17.1835066798565,0,-0.911936110500819)))
Yarns[11].AddNode(CNode(XYZ(17.1835066798565,12.5,-0.911936110500819)))
Yarns[12].AddNode(CNode(XYZ(13.5949584821026,0,-0.813927217948378)))
Yarns[12].AddNode(CNode(XYZ(13.5949584821026,12.5,-0.813927217948378)))
Yarns[13].AddNode(CNode(XYZ(17.0724919525779,0,-0.395265201421422)))
Yarns[13].AddNode(CNode(XYZ(17.0724919525779,12.5,-0.395265201421422)))
Yarns[14].AddNode(CNode(XYZ(17.1835066798565,0,-0.911936110500819)))
Yarns[14].AddNode(CNode(XYZ(17.1835066798565,12.5,-0.911936110500819)))
Yarns[15].AddNode(CNode(XYZ(15.9005186593581,0,0.328031635175211)))
Yarns[15].AddNode(CNode(XYZ(15.9005186593581,12.5,0.328031635175211)))
Yarns[16].AddNode(CNode(XYZ(17.0724919525779,0,-0.395265201421422)))
Yarns[16].AddNode(CNode(XYZ(17.0724919525779,12.5,-0.395265201421422)))
Yarns[17].AddNode(CNode(XYZ(19.3579999710847,0,0.0001)))
Yarns[17].AddNode(CNode(XYZ(19.3579999710847,12.5,0.0001)))
Yarns[18].AddNode(CNode(XYZ(15.9005186593581,0,0.328031635175211)))
Yarns[18].AddNode(CNode(XYZ(15.9005186593581,12.5,0.328031635175211)))
Yarns[19].AddNode(CNode(XYZ(10.873148126563,0,0.980558579650911)))
Yarns[19].AddNode(CNode(XYZ(10.873148126563,12.5,0.980558579650911)))
Yarns[20].AddNode(CNode(XYZ(19.3579999710847,0,0.00)))
Yarns[20].AddNode(CNode(XYZ(19.3579999710847,12.5,0.00)))
Yarns[21].AddNode(CNode(XYZ(10.873148126563,0,0.980558579650911)))
Yarns[21].AddNode(CNode(XYZ(10.873148126563,12.5,0.980558579650911)))
Yarns[22].AddNode(CNode(XYZ(0,0,-1.9419)))
Yarns[22].AddNode(CNode(XYZ(26.2,0,-1.9419)))

# Loop over all the yarns in the list
for Yarn in (Yarns[1], Yarns[2], Yarns[3], Yarns[4], Yarns[5], Yarns[6], Yarns[7], Yarns[8], Yarns[9], Yarns[10], Yarns[11], Yarns[12], Yarns[13], Yarns[14], Yarns[15], Yarns[16], Yarns[17], Yarns[18], Yarns[19], Yarns[20], Yarns[21], Yarns[22],
):

# Set the interpolation function
Yarn.AssignInterpolation(CInterpolationCubic())

# Assign a constant cross-section all along the yarn of elliptical shape
Yarn.AssignSection(CYarnSectionConstant(CSectionPowerEllipse(1.97, 0.32, 0.6, 0)))

# Set the resolution of the surface mesh created
Yarn.SetResolution(20)

# Add repeat vectors to the yarn
Yarn.AddRepeat(XYZ(50, 0, 0))
Yarn.AddRepeat(XYZ(0, 50, 0))

# Add the yarn to our textile
Textile.AddYarn(Yarn)

Yarns[23].AddNode(CNode(XYZ(0,0,0)))
Yarns[23].AddNode(CNode(XYZ(0,12.5,0)))

#yarn.AssignInterpolation(CInterpolationCubic())

YarnSections = CYarnSectionInterpNode()

Top = CSectionEllipse( 20, 1.5 )
Bottom = CSectionPowerEllipse( 20, 0.5, 0.5, 0.8 )
YarnSections.AddSection( CSectionHybrid( Top, Bottom ) )
Yarns[23].AssignSection( YarnSections )
Yarns[23].SetResolution(20)
Textile.AddYarn(Yarns[23])


# Add the textile with the name "TegGen"
AddTextile("TegGen", Textile)
Thanks in advance!

Henrik from Tallinn University of Technology
louisepb
Project Leader
Posts: 998
Joined: Tue Dec 08, 2009 2:27 pm
Location: Nottingham

Re: Please help by scripting - I am new to python

Post by louisepb »

Hi Henrik,

I've made a few changes to your script which will hopefully make sense. Get back to me if they don't.

Code: Select all

 # Script recorded by TexGen v3.5.3

# Create a textile
Textile = CTextile()

# Create a python list containing 24 yarns
Yarns = [CYarn() for i in range(23)]

# Add nodes to the yarns to describe their paths
Yarns[0].AddNode(CNode(XYZ(0,0,-1.35690511431748)))
Yarns[0].AddNode(CNode(XYZ(26.1178751920483,0,-1.10333877091771)))

Yarns[1].AddNode(CNode(XYZ(0,0,2.08283416805388)))
Yarns[1].AddNode(CNode(XYZ(7.60110970447422,0,1.75405771805033)))
Yarns[1].AddNode(CNode(XYZ(10.1506477036487,0,1.40667724159241)))
Yarns[1].AddNode(CNode(XYZ(12.7847208471295,0,0.651506576910888)))
Yarns[1].AddNode(CNode(XYZ(15.3727175975557,0,0.04466557866232)))
Yarns[1].AddNode(CNode(XYZ(18.0076219936037,0,-0.178772837708289)))
Yarns[1].AddNode(CNode(XYZ(26.1178751920483,0,-0.509198759375013)))


Yarns[2].AddNode(CNode(XYZ(0,0,2.43083185473508)))
Yarns[2].AddNode(CNode(XYZ(6.23943215714167,0,2.20845136139976)))
Yarns[2].AddNode(CNode(XYZ(9.47454545880161,0,1.80281616563909)))
Yarns[2].AddNode(CNode(XYZ(12.4162965575901,0,1.24534594008006)))
Yarns[2].AddNode(CNode(XYZ(15.7938023300884,0,0.613018734008396)))
Yarns[2].AddNode(CNode(XYZ(19.5004077270064,0,0.13923229264907)))
Yarns[2].AddNode(CNode(XYZ(26.0602739851117,0,-0.121353327941076)))

Yarns[3].AddNode(CNode(XYZ(0,0,3.02932459594996)))
Yarns[3].AddNode(CNode(XYZ(5.65085505200628,0,2.85047827962796)))
Yarns[3].AddNode(CNode(XYZ(10.0586257585936,0,2.26189529090206)))
Yarns[3].AddNode(CNode(XYZ(13.4920265998418,0,1.53749752275292)))
Yarns[3].AddNode(CNode(XYZ(16.7971553969722,0,0.981707961404881)))
Yarns[3].AddNode(CNode(XYZ(20.5534551121967,0,0.566587549982634)))
Yarns[3].AddNode(CNode(XYZ(26.0154469168219,0,0.149824303596482)))

Yarns[4].AddNode(CNode(XYZ(13.0541703920201,12.5,0.717104011532574)))
Yarns[4].AddNode(CNode(XYZ(13.0541703920201,0,0.717104011532574)))
Yarns[5].AddNode(CNode(XYZ(13.5949584821026,0,-0.120682621971694)))
Yarns[5].AddNode(CNode(XYZ(13.5949584821026,12.5,-0.120682621971694)))
Yarns[6].AddNode(CNode(XYZ(13.0541703920201,0,0.717104011532574)))
Yarns[6].AddNode(CNode(XYZ(13.0541703920201,12.5,0.717104011532574)))
Yarns[7].AddNode(CNode(XYZ(10.6734244282232,0,-0.82239746086376)))
Yarns[7].AddNode(CNode(XYZ(10.6734244282232,12.5,-0.82239746086376)))
Yarns[8].AddNode(CNode(XYZ(13.5949584821026,0,-0.120682621971694)))
Yarns[8].AddNode(CNode(XYZ(13.5949584821026,12.5,-0.120682621971694)))
Yarns[9].AddNode(CNode(XYZ(13.5949584821026,0,-0.813927217948378)))
Yarns[9].AddNode(CNode(XYZ(13.5949584821026,12.5,-0.813927217948378)))
Yarns[10].AddNode(CNode(XYZ(10.6734244282232,0,-0.82239746086376)))
Yarns[10].AddNode(CNode(XYZ(10.6734244282232,12.5,-0.82239746086376)))
Yarns[11].AddNode(CNode(XYZ(17.1835066798565,0,-0.911936110500819)))
Yarns[11].AddNode(CNode(XYZ(17.1835066798565,12.5,-0.911936110500819)))
Yarns[12].AddNode(CNode(XYZ(13.5949584821026,0,-0.813927217948378)))
Yarns[12].AddNode(CNode(XYZ(13.5949584821026,12.5,-0.813927217948378)))
Yarns[13].AddNode(CNode(XYZ(17.0724919525779,0,-0.395265201421422)))
Yarns[13].AddNode(CNode(XYZ(17.0724919525779,12.5,-0.395265201421422)))
Yarns[14].AddNode(CNode(XYZ(17.1835066798565,0,-0.911936110500819)))
Yarns[14].AddNode(CNode(XYZ(17.1835066798565,12.5,-0.911936110500819)))
Yarns[15].AddNode(CNode(XYZ(15.9005186593581,0,0.328031635175211)))
Yarns[15].AddNode(CNode(XYZ(15.9005186593581,12.5,0.328031635175211)))
Yarns[16].AddNode(CNode(XYZ(17.0724919525779,0,-0.395265201421422)))
Yarns[16].AddNode(CNode(XYZ(17.0724919525779,12.5,-0.395265201421422)))
Yarns[17].AddNode(CNode(XYZ(19.3579999710847,0,0.0001)))
Yarns[17].AddNode(CNode(XYZ(19.3579999710847,12.5,0.0001)))
Yarns[18].AddNode(CNode(XYZ(15.9005186593581,0,0.328031635175211)))
Yarns[18].AddNode(CNode(XYZ(15.9005186593581,12.5,0.328031635175211)))
Yarns[19].AddNode(CNode(XYZ(10.873148126563,0,0.980558579650911)))
Yarns[19].AddNode(CNode(XYZ(10.873148126563,12.5,0.980558579650911)))
Yarns[20].AddNode(CNode(XYZ(19.3579999710847,0,0.00)))
Yarns[20].AddNode(CNode(XYZ(19.3579999710847,12.5,0.00)))
Yarns[21].AddNode(CNode(XYZ(10.873148126563,0,0.980558579650911)))
Yarns[21].AddNode(CNode(XYZ(10.873148126563,12.5,0.980558579650911)))
Yarns[22].AddNode(CNode(XYZ(0,0,-1.9419)))
Yarns[22].AddNode(CNode(XYZ(26.2,0,-1.9419)))

# Loop over all the yarns in the list
for Yarn in Yarns:

	# Set the interpolation function
	Yarn.AssignInterpolation(CInterpolationCubic())

	# Assign a constant cross-section all along the yarn of elliptical shape
	Yarn.AssignSection(CYarnSectionConstant(CSectionPowerEllipse(1.97, 0.32, 0.6, 0)))

	# Set the resolution of the surface mesh created
	Yarn.SetResolution(20)

	# Add repeat vectors to the yarn
	Yarn.AddRepeat(XYZ(50, 0, 0))
	Yarn.AddRepeat(XYZ(0, 50, 0))

	# Add the yarn to our textile
	Textile.AddYarn(Yarn)

Yarns.append( CYarn() )
Yarns[23].AddNode(CNode(XYZ(0,0,0)))
Yarns[23].AddNode(CNode(XYZ(0,12.5,0)))

Yarns[23].AssignInterpolation(CInterpolationCubic())

# Create hybrid section
Top = CSectionEllipse( 20, 1.5 )
Bottom = CSectionPowerEllipse( 20, 0.5, 0.5, 0.8 )
Section = CSectionHybrid( Top, Bottom ) 

# Set interpolation between nodes and set section for each node
NumNodes = Yarns[23].GetNumNodes()
YarnSections = CYarnSectionInterpNode()
for i in range(NumNodes):
	YarnSections.AddSection( Section )
	
Yarns[23].AssignSection( YarnSections )
Yarns[23].SetResolution(20)
Textile.AddYarn(Yarns[23])


# Add the textile with the name "TegGen"
AddTextile("TegGen", Textile)
Hope that helps,
Louise
Henrik.Herranen
Posts: 4
Joined: Mon May 20, 2013 11:48 am

Re: Please help by scripting - I am new to python

Post by Henrik.Herranen »

Hello Louise,

Thanks again for kind help!

Henrik
Henrik.Herranen
Posts: 4
Joined: Mon May 20, 2013 11:48 am

Re: Please help by scripting - I am new to python

Post by Henrik.Herranen »

Hi,

Next question:

I want to define variable cross-section to yarns, is this the following correct sentence(I found it from one previous posts, but it has not been confirmed by anyone)? How can I define the diffrenet sections at nodes ?
Jean-Vincent wrote:Thanks both of you for reply.

Your are right this is due to the lenght and the global dimension of the mandrel. But it is difficult for me to use a little part and reproduice it as you said because my scripts can cover irregular diameters for exemple.

But, I succed to solve a part of the problem by using the "SetResolution()". Now I am able to generate what I need but of course it takes time to do it (few minutes, max 5min).

My last problem is to set the yarn section for each node in the script, I try like that :

Code: Select all

section1 = CSectionEllipse(yarn_width, yarn_thickness)

for j in range(yarn_number):
    nodesection=CYarnSectionInterpNode(j,0)
    for i in range(2*yarn_number):
        section=section1
        yarnsection=nodesection.AddSection(section)
    Textile.GetYarn(j).AssignSection(yarnsection)
I am not sure that is clear but i have to ask my supervisor before share the all script. Actually he is in holliday, but for me i think it is normal to share with the communauty and especially in my case because I ask help :).

Once again thanks.

Thanks for the help, still Python is kind of struggle to learn, since I have had previously pretty much only VBA experience :)
louisepb
Project Leader
Posts: 998
Joined: Tue Dec 08, 2009 2:27 pm
Location: Nottingham

Re: Please help by scripting - I am new to python

Post by louisepb »

Hi Henrik,

Basically you can do it in the way that you have already done it for yarn 23. (Quite similar to the code snippet you found). This is the section which does this:

Code: Select all

YarnSections = CYarnSectionInterpNode()
for i in range(NumNodes):
   YarnSections.AddSection( Section )
   
Yarns[23].AssignSection( YarnSections )
If you want different sections at each node then the Section passed to AddSection will be different in each case.

If the sections have already been specified at nodes but are all the same then you could make a copy of the YarnSection for the yarn, use ReplaceSection to change any that need to be changed and then call AssignSection with the modifed YarnSection.

Hope that helps,
Louise
Henrik.Herranen
Posts: 4
Joined: Mon May 20, 2013 11:48 am

Re: Please help by scripting - I am new to python

Post by Henrik.Herranen »

Hi again TexGen enthusiasts and especially Louise!

I am trying now to create a yarn that has variable crossections with periodically repeating pattern for section. I have composed following code:

Code: Select all

# Script recorded by TexGen v3.5.3

# Create a textile
Textile = CTextile()

# Create a python list containing 1 yarn
Yarns =[]
Yarns.append(CYarn())
# Add nodes to the yarns to describe their paths
mylist16z = [-1.941931086,-1.879873111,-1.825708936,-1.779437069,-1.741056237,-1.710565382,-1.687963664,-1.673250463,-1.666425372,-1.667488205,-1.677077896]
NumNodes = range(11)
k=0
for number in NumNodes:
	Yarns[0].AddNode(CNode(XYZ(k,0, mylist16z[number])))
	k += 2.6043500
	
YarnSection = CYarnSectionInterpNode()
Nodes = Yarns[0].GetMasterNodes()
for Node in Nodes:
	if Node in (3, 4, 7, 9):
		Section =(CSectionPowerEllipse(2.2, 0.32, 0.6, 0))
	else :
		Section =(CSectionPowerEllipse(1.97, 0.32, 0.6, 0))
	YarnSection.AddSection(Section)
	Yarns[0].AssignInterpolation(CInterpolationCubic())
	
Yarns[0].AssignSection(YarnSection)
Yarns[0].SetResolution(20)
Textile.AddYarn(Yarns[0])
AddTextile("TegGen", Textile)
Textile2 = CTextile()

Question is: why doesnt it make the section variable?

Tnx in Advance!

Henrik
louisepb
Project Leader
Posts: 998
Joined: Tue Dec 08, 2009 2:27 pm
Location: Nottingham

Re: Please help by scripting - I am new to python

Post by louisepb »

Hi Henrik,

The problem is with the line "if Node in (3, 4, 7, 9):" - Node is a class ( see the API reference here: http://texgen.sourceforge.net/api/class ... _node.html ) and will therefore never be the values checked for in the test. I've changed the code below to set up a counter for which the test will work.

Code: Select all

# Create a textile
Textile = CTextile()

# Create a python list containing 1 yarn
Yarns =[]
Yarns.append(CYarn())
# Add nodes to the yarns to describe their paths
mylist16z = [-1.941931086,-1.879873111,-1.825708936,-1.779437069,-1.741056237,-1.710565382,-1.687963664,-1.673250463,-1.666425372,-1.667488205,-1.677077896]
NumNodes = range(11)
k=0
for number in NumNodes:
   Yarns[0].AddNode(CNode(XYZ(k,0, mylist16z[number])))
   k += 2.6043500
   
YarnSection = CYarnSectionInterpNode()
Nodes = Yarns[0].GetMasterNodes()
i = 1
for Node in Nodes:
   if i in (3, 4, 7, 9):
      Section =(CSectionPowerEllipse(2.2, 0.32, 0.6, 0))
   else :
      Section =(CSectionPowerEllipse(1.97, 0.32, 0.6, 0))
   YarnSection.AddSection(Section)
   Yarns[0].AssignInterpolation(CInterpolationCubic())
   i = i+1;
   
Yarns[0].AssignSection(YarnSection)
Yarns[0].SetResolution(20)
Textile.AddYarn(Yarns[0])
AddTextile("TegGen", Textile)
Textile2 = CTextile()
Best regards,
Louise
Post Reply