Engineering, Analysis and Design

Saturday, May 18, 2024

Random Edge Generation Using SpaceClaim Python API

Python is arguably one of the easiest and most popular programming languages. Particularly, It is a lifesaver for those who, like us, only learn VB in the University and forget to encode it after a while. The current default solid modelling tools in the CAD software will not respond to the demands with the increase of geometric complexity. Grasshopper users must have already figured out what exactly we would like to mention. However, Today  We will only mention SpaceClaim. Since, as known the company of SC  has taken long miles since Ansys invested in the company. Also, It is a key source for those who put particular attention to both Finite Element Analysis (FEA)  and topology optimisation.

1.Here we go!

Once the software is opened, select Groups section. Right click and  “Create Script Group”  and access code view by clicking edit.

PS: A quick reminder, Python API, works via IronPython. Make you sure which version you use.

Python IDLE is opened as the figure above and we can type the codes on this screen. All the types you wrote will be recorded as a code when you click the record (red dot) button.

Check your version by importing a Sys.

2.7.4 (IronPython 2.7.4 ( on .NET 4.0.30319.42000 (64-bit))

2. In order to open and save a file import an OS

import os

The folder we work under has been changed with os.chdir.

3. SpaceClaim

At the beginning of the article, we mentioned that random beams will be generated. However, essentially let us detect how SpaceClaim draws a beam.  Enable the record button and draw a line.

SpaceClaim drew a line by following the steps below;

  • Create a plane (intersects with x and z-axis)
  • Determine the coordinates of start and end points
  • Draw the line using SketchLine command.

4. However, by thinking differently there is another way to do it.

Generate the same line defining two multiaxis points.

point1 = Point.Create(MM(a),MM(b),MM(c))
point2 = Point.Create(MM(a2),MM(b2),MM(c2))
line = SketchLine.Create(point1,point2)

As you can see that It is getting obvious how the random beam generation will be done.

First define a Randomiser

import random
a = random.sample(range(0,1000), 50)
b = random.sample(range(0,500), 50)
c = random.sample(range(0,2000), 50)

Three lists have been created with different amplitudes.

5. Create random points among the random numbers.

list1 = list()
t = 0
while t <= 100:
    t = t + 1
    pt = "pt" + String(t)
    cz = "cz" + String(t)
    pt = Point.Create(MM(random.choice(a)),MM(random.choice(a)),...
    cz = SketchLine.Create(random.choice(list1),random.choice(list1))

First, we created a list. We will record our points here. We have assigned an initial value with  “t = 0 “.  With the variable of “while t≤100”, the lines will be drawn until the variable of t is 100. To prevent the lines from repeating each other, we write the line names as pt, pt1, pt2, and we print them into the table. Finally, SketchLine allows you to draw two points from the list and draw a line between them.

6. Printing the Coordinates

In order to view the point coordinates, it is enough to type “print (list1)” . When we run the code line, the program will start creating lines. The processing time may increase depending on the processor speed.

Show More

Leave a Reply

Your email address will not be published. Required fields are marked *