Skip to main content

Selection In Headless

The headless API is intended to represent interactive CAD tasks in program code. This generally means we have no GUI where we can interact with. We describe our intentions with code, line by line. This has a lot of advantages, but there are also difficulties we have to deal with. Especially tasks where we normally select geometry with the cursor, is not practicable. In this cases we would have to know where the exact position of geometry is found in the global coordinate system. This is hardly possible.

For this task we have implemented a method, which allows us to interact with the graphic on the screen. The method is called findOrSelect and can be found in the documentation of the History API.

The notation of the method means that we can use the method to find the geometry by global coordinates but also by a selection. When creating a model by code for the first time we certainly use selection to get the geometry. For all of the selected geometries, we will get the exact global position printed in the debug console. This output we can use later to give it as a parameter to the method, because when developing a model we run the same code a few times and we don't want to select the same edge or face each run. With this exact position, the algorithm will find the edge or face by itself and we can concentrate on other code.

Here is a code snippet how to use the findOrSelect method for the first time.

const edges = await api.findOrSelect(part, BrepElemType.EDGE, 2, null)

To see the exact documentation of the method see findOrSelect(...). To sum up it means that we want to select interactively 2 edges in the part.

After running through this code we will see this output in the debug console.

drawing

Output in debug console after selecting two edges

This nested array containing points returns the exact position of the selected geometry and means we can copy and paste the array into the last parameter of the findOrSelect Method. Instead of null we use [[{ x: -80, y: 0, z: 110}],[{ x: -95, y: 0, z: 110}]] to call findOrSelect. See following snippet:

const edges = await api.findOrSelect(part, BrepElemType.EDGE, 2, [[{ x: -80, y: 0, z: 110}],[{ x: -95, y: 0, z: 110}]])

The result is exactly the same as we would have selected the geometry. We will get the id's of two edges. We can use this id's for further operations like fillets, work axis, etc...

See the selection in action in the next video.

drawing

Selection in headless API using findOrSelect() method

You can find the whole example in our public application buerli-examples