Using the Ansys Discovery Combine tool via scripting

Options
M
M Member, Employee Posts: 230
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
edited March 27 in Structures

The combine tool in Discovery allows you to do various merge/combine actions with bodies in your model. You can get started by checking out the AIS regarding Discovery and Combine: https://courses.ansys.com/index.php/courses/geometry-prep-for-cfd-using-ansys-discovery/lessons/how-to-use-combine-tool-in-ansys-discovery-lesson-4/

But what you do via the GUI you can usually do with scripting also.

The example below is just a demo how to combine/merge two bodies in Discovery. The first part of the script just generates the bodies. These end up in one component to avoid Discovery automatically merging them.

the second part uses the names of the bodies (that ended up in a component) to merge.

Note that Discovery will automatically merge bodies which is why I moved them to a component to avoid this (until the script merges them).

# Python Script, API Version = V232
## 2 body problem
ClearAll()
Selection.Clear()

# Set up some python names/lists/dicts
p1 = 'board'
p2 = 'comp1'
part_dict = {}
part_list = [p1,p2]

# Create some geometry
part1_dim = [100,50,10] # length, width and height of part 1
part2_dim = [50,25,10] # length, width and height of part 2

# For a 2 body demo with all bodies centered on 0,0
# Body 1, create, select, rename, move to component
x1 = part1_dim[0]/2
y1 = part1_dim[1]/2
z1 = part1_dim[2]
P0 = Point.Create(MM(-x1),MM(-y1),MM(0))
P1 = Point.Create(MM(x1),MM(y1),MM(z1))
part_1 = BlockBody.Create(P0,P1)

selection1 = Selection.Create(part_1.CreatedBody)
result = RenameObject.Execute(selection1,p1)

#Discovery will automatically merge bodies if they are not created in different components.
ComponentHelper.MoveBodiesToComponent(selection1)

# Body 2 create, select, rename, move to component
x2 = part2_dim[0]/2
y2 = part2_dim[1]/2
z2 = part1_dim[2] +  part2_dim[2]
P0 = Point.Create(MM(-x2),MM(-y2),MM(z1))
P1 = Point.Create(MM(x2),MM(y2),MM(z2))
part_2 = BlockBody.Create(P0,P1)
selection2 = Selection.Create(part_2.CreatedBody)
result = RenameObject.Execute(selection2,p2)
selections = selection2
component = GetRootPart().Components[0]
result = ComponentHelper.MoveBodiesToComponent(selections, component, False)

# All bodies are now in the component named component...
for body in component.GetAllBodies():
    if body.GetName() in part_list:
        part_dict[body.GetName()] = body
    print(body.GetName())

# The last two lines uses the dictionary to combine (combine tool) the two bodies in the component
targets = BodySelection.Create(part_dict[p1], part_dict[p2])
result = Combine.Merge(targets)

A much more comprehensive introduction to scripting in Discovery can be found at: https://courses.ansys.com/index.php/courses/scripting-in-ansys-discovery/