User Guide
Introduction
There are several ways to interface with TexGen. A little bit of technical information is necessary to understand how this all fits together. The core TexGen functionality has been written in C++, on top of this a Python interface exists that has access to all the C++ functions of TexGen. There are many advantages of a Python interface that may not be immediatly obvious to the end user.
A user friendly Graphical User Interface has been built on top of the Python interface to provide a quick and easy method to interface with TexGen. But the real power comes with writing small Python scripts which will be discussed later.
Graphical User Interface
...
Python Interface
Python is a high level interpreted programming language. In this case it is used as a scripting language to allow easy access to TexGen functionality. Python has been embedded in the TexGen GUI which allows the GUI to run python scripts. It is not the goal of this tutorial to teach Python programming. There are many resources online for that. A few of them are listed below:
- Learn Python in 10 minutes - Quick introduction to Python for competent programmers.
- Python Tutorial - Official Python tutorial (requires some programming knowledge).
- How to Think Like a Computer Scientist: Learning with Python - Book on Python programming for beginners. HTML Version
- Dive Into Python - In depth book covering advanced subjects in a comprehensive manner. HTML Version
An advanced knowledge of Python is not needed in order to write simple TexGen scripts. However, bear in mind that Python is a very powerful language with many libraries included for performing common tasks. I suggest you try to follow through the examples below and if you get stuck refer to one of the documents above.
Example 1
First of all, a Python script is just a plain ASCII text file with a .py extension. To create one simple open up your favorite text editor. Notepad will do if you have nothing else but I suggest a freeware text editor like PSPad. Code that goes into the python script will be displayed like so:
# All Python code will be shown in a box like this one. # Lines begining with '#' character are comments
In this example, we will create a simple 2x2 plain weave model with a Python script. Its time to dive in and see some actual Python code. At this point if you are completely lost please refer back to the resources on Python programming. The first step is to create an instance of the CTextileWeave2D
class and give it a name, weave
:
# Create a 2D weave textile weave = CTextileWeave2D(2, 2, 1, 0.2, True)
The CTextileWeave2D
class takes 5 parameters in the order shown below:
- Number of weft yarns in the unit cell
- Number of warp yarns in the unit cell
- Spacing between the yarns
- Thickness of the fabrics
- Refine model (True/False)
The only parameter that really needs an explanation here is the Refine model option, this controls whether or not the interference correction algorithm will be applied to keep the yarn volumes from intersecting which is a common problem. The interference correction algorithm generally does a good job but is not instantaneous so sometimes it can be beneficial to switch it off.
This is the bare minimum information necessary to create a 2D woven fabric. In the following steps we will add more information to create the model the way we want it.
Let's set the weave pattern, this determines whether a warp or weft yarn will appear on top at a particular cross over. Cross overs are arranged in 2d grid where the size of the grid depends on the number of warp and weft yarns created. To swap the positions of the weft and warp yarns, we call the function SwapPosition
with the grid coordinates of the cross over. In order to create a plain weave we need to do this on two diagonally opposite cross overs like so:
# Set the weave pattern weave.SwapPosition(0, 0) weave.SwapPosition(1, 1)
Now let's add our weave
model to the database of models so that TexGen can render it:
# Add the textile AddTextile(weave)
Thats it! This is a very simple script but it will generate a plain weave model. To recap your Python script should look like this:
# Create a 2D weave textile weave = CTextileWeave2D(2, 2, 1, 0.2, True) # Set the weave pattern weave.SwapPosition(0, 0) weave.SwapPosition(1, 1) # Add the textile AddTextile(weave)
Save it to disk as PlainWeave.py or give it whatever name you want as long as it has a .py extension.
In order to test the Script we need to load it into TexGen. So start up the TexGen GUI and from the Python menu select Run Script. Select the file you have just saved and click ok. With a bit of luck you should see a 2d plain weave on screen.
Congratulations you have created your first TexGen Python script!
Example 2
In the first example we made use of the CTextileWeave2D
which is the reason the script is so compact. This was great for creating a 2D Weave, but what if we want to create something other than a 2D Weave? In this example we will create yarns directly rather than through one of the textile sub-classes.
First we will define a general textile and call it Textile
:
# Create a textile Textile = CTextile()
The CTextile
class doesn't take any parameters, instead yarns will be created and added to it. Next we will create 4 yarns to go inside our textile, it is convenient to make use of the Python lists to do this. Create a list called Yarns
with 4 elements, each one an instance of the class CYarn
:
# Create a python list containing 4 yarns Yarns = [CYarn(), CYarn(), CYarn(), CYarn()]
Yarns
now contains 4 empty yarns. Note that the yarns are not currently associated with the Textile, first we must fill in the details then make them a part of the textile. The first thing to do is define their paths with nodes. This is done like so:
# Add nodes to the yarns to describe their paths Yarns[0].AddNode(CNode(XYZ(0, 0, 0))) Yarns[0].AddNode(CNode(XYZ(0.22, 0, 0.05))) Yarns[0].AddNode(CNode(XYZ(0.44, 0, 0))) Yarns[1].AddNode(CNode(XYZ(0, 0.22, 0.05))) Yarns[1].AddNode(CNode(XYZ(0.22, 0.22, 0))) Yarns[1].AddNode(CNode(XYZ(0.44, 0.22, 0.05))) Yarns[2].AddNode(CNode(XYZ(0, 0, 0.05))) Yarns[2].AddNode(CNode(XYZ(0, 0.22, 0))) Yarns[2].AddNode(CNode(XYZ(0, 0.44, 0.05))) Yarns[3].AddNode(CNode(XYZ(0.22, 0, 0))) Yarns[3].AddNode(CNode(XYZ(0.22, 0.22, 0.05))) Yarns[3].AddNode(CNode(XYZ(0.22, 0.44, 0)))
We have added 3 nodes to each yarn creating a woven pattern. The number in the square brackets after Yarns
refers to a particular yarn in the list. The function AddNode
is then called for the yarn. The function takes an instance of the class CNode
which in turn is constructed with an instance of the class XYZ
representing the position of the node. The class XYZ
is constructed with 3 numbers representing the X, Y and Z coordinates of the point.
We still need to define a few more things to obtain a model. In this case we will make all our yarns the same except for their paths of course. The easiest way to do this is to loop over the yarns and assign the same properties to each yarn one by one. The syntax for looping over the Python list Yarns
is shown here:
# Loop over all the yarns in the list for Yarn in Yarns:
Inside the loop, the variable Yarn
refers to the current yarn in the list. So within the loop we just assign properties to Yarn
and they will be applied to all the yarns in the list. In python the begining and end of a loop are defined by indentation, hence the following lines are indented to represent the contents of the loop.
Let us define periodic cubic spline interpolation between the nodes:
# Set the interpolation function Yarn.AssignInterpolation(CInterpolationCubic())
This is done with the function AssignInterpolation
which takes an instance of a class derived from CInterpolation
. In this case we used CInterpolationCubic
to specify periodic cubic spline interpolation.
Next we need to define a cross section for the yarn, to keep things simple a constant cross section all along the length of the yarn will be defined:
# Assign a constant cross-section all along the yarn of lenticular shape Yarn.AssignSection(CYarnSectionConstant(CSectionEllipse(0.18, 0.04)))
The function AssignSection
takes an instance of a class derived from CYarnSection
. In this case we used CYarnSectionConstant
which in turns take an instance of a class derived from CSection
as a constructor. CSectionEllipse
defines a lenticular cross section with given width and height defined by the first and second parameters respectively.
# Set the resolution of the surface mesh created Yarn.SetResolution(20)
This line sets the resolution of the surface mesh created. The first parameter to the function SetResolution
represents the number of points around a cross section. The number of points along the length of the yarns is automatically calculated such that the distance between points is similar to around the cross section.
Next we must add repeat vectors to define how the yarns are repeated.
# Add repeat vectors to the yarn Yarn.AddRepeat(XYZ(0.44, 0, 0)) Yarn.AddRepeat(XYZ(0, 0.44, 0))
The function AddRepeat
takes an instance of the class XYZ
. As we saw earlier the class XYZ
is constructed with 3 numbers defining the X, Y and Z components.
Finally the yarn is fully defined, it can now be added to Textile
:
# Add the yarn to our textile Textile.AddYarn(Yarn)
This is the end of the loop, hence the following lines return to the original indentation. Now lets define a Domain within which the textile will be placed:
Textile.AssignDomain(CDomainPlanes(XYZ(0, 0, -0.02), XYZ(0.44, 0.44, 0.07)))
The function AssignDomain
takes an instance of a class derived from CDomain
. In this case we will create a box shaped domain, this can be accomplished by using the class CDomainPlanes
. A box can be created by passing in opposite corners of the box (the minimum and maximum coordinates). This is done again with the class XYZ
.
And finally Textile
must be added to so that it can be rendered. Each Textile added must be assigned a name to identify it, this is the name that will appear at the top in the TexGen GUI. This name can either be generated automatically or we can specify it explicitly like so:
# Add the textile with the name "polyester" AddTextile("polyester", Textile)
That's it! You now have the knowledge to create any type of fabric imaginable. Well, almost. The model created here is pretty simple, there is plenty of oppertunity to create more complex shapes. For example non-constant cross sections can be defined, complex domain shapes defined by arbritrary planes, different interpolation functions.
Here is the completed script:
# Create a textile Textile = CTextile() # Create a python list containing 4 yarns Yarns = [CYarn(), CYarn(), CYarn(), CYarn()] # Add nodes to the yarns to describe their paths Yarns[0].AddNode(CNode(XYZ(0, 0, 0))) Yarns[0].AddNode(CNode(XYZ(0.22, 0, 0.05))) Yarns[0].AddNode(CNode(XYZ(0.44, 0, 0))) Yarns[1].AddNode(CNode(XYZ(0, 0.22, 0.05))) Yarns[1].AddNode(CNode(XYZ(0.22, 0.22, 0))) Yarns[1].AddNode(CNode(XYZ(0.44, 0.22, 0.05))) Yarns[2].AddNode(CNode(XYZ(0, 0, 0.05))) Yarns[2].AddNode(CNode(XYZ(0, 0.22, 0))) Yarns[2].AddNode(CNode(XYZ(0, 0.44, 0.05))) Yarns[3].AddNode(CNode(XYZ(0.22, 0, 0))) Yarns[3].AddNode(CNode(XYZ(0.22, 0.22, 0.05))) Yarns[3].AddNode(CNode(XYZ(0.22, 0.44, 0))) # 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 lenticular shape Yarn.AssignSection(CYarnSectionConstant(CSectionEllipse(0.18, 0.04))) # Set the resolution of the surface mesh created Yarn.SetResolution(20) # Add repeat vectors to the yarn Yarn.AddRepeat(XYZ(0.44, 0, 0)) Yarn.AddRepeat(XYZ(0, 0.44, 0)) # Add the yarn to our textile Textile.AddYarn(Yarn) # Create a domain and assign it to the textile Textile.AssignDomain(CDomainPlanes(XYZ(0, 0, -0.02), XYZ(0.44, 0.44, 0.07))) # Add the textile with the name "polyester" AddTextile("polyester", Textile)