9.7 Integrating Visualize with Parasolid and HOOPS Exchange
Visualize supports the subtraction boolean operation via the native Parasolid API. To perform a subtraction in a scene, we'll load two CAD models into a scene, defining one of them as a target (the 3D model we'll be subtracting from) and the other as a tool (the 3D shape that will define the bounds of the subtraction operation). In this example, we'll load an engine model as our target and then load a cylinder to serve as the tool for the subtraction.
Please note, this is a very basic sample boolean subtraction operator, merely a starting point from which to build a more robust boolean operator leveraging Parasolid. Specifically, this operator implementation does not address the case in which a boolean operation would result in multiple bodies (although the comments in the operator code explain how to address that eventuality).
The full code for importing two CAD models into a scene is available below, and, for the most part, we'rejust using a standard approach for importing files in Visualize. However, please note that we're using a Exchange::ImportOptionsKit:SetLocation() to import our second model (the tool) into the same Component tree as the first file.
In addition, please note that – in terms of positioning – our models have been designed to overlap each other in World Space to facilitate the subtraction operation. If you want to use two models that weren't necessarily designed for this purpose, there are a variety of approaches for positioning them on top of each other to perform the boolean operation. For example, using the Handles Operator is an easy way to manually super-impose one model on top of another.
Once loaded, our two CAD models will look like this, with the cylinder piercing the engine model on the left:
Defining a custom operator
Perhaps the simplest method for implementing a boolean operation in Visualize is to define a custom operator that handles a series of mouse clicks for defining the target and tool. The full source code for the class is available at the bottom of the page, but for now we'll just focus on the OnMouseUp method, since that's where the bulk of the logic is contained. (More info about custom operators is available in this section.)
We'll call our custom operator class TestBooleanOperator, and like all custom operators, it inherits the HPS::Operator class. In this example, we'll overload the Operator::OnMouseUp() method, and in the overloaded method we'll place the logic for selecting the target and tool for the boolean subtraction operation:
The Parasolid Body upon which the user clicks first will be selected as the target, and the second body selected by the user will be the tool for the boolean operation:
Instantiating the custom operator
With our custom operator class defined, in our application code (provided in full below) we can now instantiate our custom operator and push it onto the operator stack in the Default priority queue:
This instance of TestBooleanOperator is now available in the operator stack, and we'll learn how to retrieve it in the next section. (For more details about the operator stack, please see this section.)
Once our TestBooleanOperator instance has been pushed to the operator stack, Visualize takes ownership of its memory, so do not call delete on this instance, as it will be automatically freed by Visualize.
Retrieving the operator from the stack
Suppose that after we've pushed our original instance of TestBooleanOperator onto the operator stack, we want to access its member methods – in this case, specifically the ApplyBoolean() method.
In order to retrieve our custom operator from the Default priority operator stack, we can use the following approach:
At this point, however, in order to gain access to the methods in our derived TestBooleanOperator class we'll need to create a new pointer to our derived type and use a static_pointer_cast to transform our OperatorPtr to a shared pointer of TestBooleanOperator type.
Now that we have a TestBooleanOperator pointer, we can access its methods and call the ApplyBoolean() function, which will call Parasolid's boolean subtraction function, removing from the target any overlapping volume, as seen in the image below:
After the data has been changed in Parasolid, Visualize needs to be updated. This update can be accomplished by using the three functions: Tessellate() (for geometry that was modified, like the target), Delete() (for geometry that was deleted, like the tool), and AddEntity() (for geometry that was created, which would happen in the case where a boolean operation splits an entity in two, for example).
Full Source for Custom Boolean Operator
TestBooleanOperator derived class
Scene setup and operator instantiation
Operator retrieval and boolean operation execution