My First Part
This tutorial will teach you how to create the first part with the History API. We will start with a simple cylinder. Then, to get an idea about what's possible with the History API, we're going to make the model more and more complex.
Let's follow these steps:
- Create a simple cylinder
- Add some more features
- Add expressions to make the model configurable
- Save the model in native file format
#
1. Create a simple cylinderFirst, you must create a new code environment where you will make your first example.
- Create a copy of templateCreator.ts in the history folder and name it, for example, cylinder.ts
- You will now have an empty create method where you can put code into it.
Before we can start creating the cylinder and other features, we need to create an empty part. In this part we put all our features into.
- Create an empty part and name it "CylinderPart"
- Create a cylinder with diameter = 50 and height = 100. We don't set any reference now, let the brackets empty.
As result you will see a simple cylinder.
#
2. Add some more featuresLet's add some more features to see what we can do with it. Maybe we could add another flat cylinder and one to subtract from the others, and then we get something like a flange. Follow the next steps:
- Create a big flat cylinder with height = 30 and diameter = 310
- Unite this new cylinder with the first one (Boolean Operation)
- Create another cylinder with diameter = 30 and height = 100, which we will use for subtraction.
- Subtract the created cylinder from the unified one.
Let's make the flange look a bit more like a genuine flange. We should add some chamfers on the edges and some holes to the bottom cylinder for the screws. To set chamfers on it, we need to know the edge ids. The edge ids can be found by using the selectGeometry() method.
Add this code to the file and see what happens with the flange.
As you can see, the code stops within the selectGeometry() method. You're able to make selections on the current state of the model. If you hover over the model, you can see which kind of selections are allowed. In this case only arc and circle selections are allowed. To get more information about the selectGeometry() method, see this chapter here.
Now let's add some holes to bottom cylinder.
💡 Tip: Use linear or circular pattern to create the same feature multiple times in a row or around an axis
Follow the next steps:
- Create a work coordinate system (WCS) at the position where we want to create the hole
- Create a new cylinder with diameter = 30, height = 50 and the reference will be the WCS that we created the step before
- Create a work axis in the middle of the cylinders. That means position = {0,0,0} and normal = {0,0,1}
- Create a circular pattern, use the cylinder from the steps before to add it 4 times by an angle of 90°. Merge the solids.
- Create a subtraction between the flange and the patterned cylinders to create the holes
#
3. Add expressions to make the model configurableIn part modeling, we often work with expressions to make the part configurable. Expressions are like global variables. Easy to update, and they can depend on each other. That means we can change only one expression, but many others might also change, making the model look quite different. To get more information about using expressions, see chapter Expressions
Let's try it out and make the flange configurable with expressions.
- Create an expression for thickness = 30 and the diameter = 190 of the upper cylinder (upperCylDiam). These two expressions we want to control directly.
- Create a depending expression for the hole diameter of the flange (upperCylHoleDiam = upperCylDiam - thickness)
- Create a depending expression for the diameter of the lower flat cylinder (lowerCylDiam = upperCylDiam + 120)
- Create a depending expression for the offset of the circular holes (holeOffset = upperCylDiam + 60)
- Add the created expression to the right features in the code.
- We also changed the naming slightly to make it easier to understand.
See the following code snippet:
#
4. Save the model in native file formatUse the following code snippet and copy it at the end of your example:
When you get to the code at runtime, you will be prompted to save the file on you local file system.
After saving this part in native file format ofb, we can load it with Buerligons and see the whole part history, including expressions. Later we will see how we can quickly develop configurators by code and update expressions by a single line of code with a significant effect on the part.