10 Replies Latest reply on Feb 26, 2018 1:48 PM by J. R.

# Macro to determine the distance to the furthest point from a plane?

Hello,

I am building a macro, and a part of that macro has to determine the distance from a specific plane to the furthest point of any solid body that is in the model. Here's a simplified screenshot to help visualize the problem:

The real difficulty here is that there might be all sorts of solid bodies, not necessarily the one depicted in the screenshot. There might be multiple bodies as well, and they might be moved, rotated, re-scaled too, so binding the macro to follow a specific point or edge will not work.

The best "solution" I found so far is using that macro to create an offset plane from that main plane, use it surface-cut half of the body, and then advance the offset plane by small steps until surface cut fails, using the latest offset distance as the number I'm looking for. As you can imagine, this is neither practical, nor accurate - I need an accurate solution that works in a single rebuild cycle, not a dozen.

I looked into Bounding Box functionality, but it is very heavy on performance, and isn't really convenient for this challenge.

Ideally, I want to write a single function, which, when called, returns the distance from the selected plane to that furthest point on any solid body in the model. Using auto-generated sketches or other kinds of reference geometry is fine, as long as it doesn't add extra an rebuild cycle, and doesn't slow down the performance of the macro. Can anybody give any ideas how to do this?

P.S. I forgot to say that I have thought of another promising method - create a Boss Extrude feature from that reference plane, with depth set to "Through All", without merging, then measure the height of this extrusion, and finally delete it. However, Through All option doesn't provide a height in a number, so the usual myDimension = Part.Parameter("D1@Boss-Extrude1") won't work.

• ###### Re: Macro to determine the distance to the furthest point from a plane?

I have no idea how to do this but i know when you go into "edit scene" in the settings for the floor there is an "offset to geometry" option. not sure if there is any API that can take advantage of this functionality...

• ###### Re: Macro to determine the distance to the furthest point from a plane?

Just spitballin'...

You could iterate through all the vertices pretty quickly.  Are your models such that the furthest point(s) will always be a vertex, or could it be a round edge or dome surface?

Otherwise you could use the Extrude method you mentioned, get the faces belonging to that extrude.  Find the face that is parallel to your original plane, and measure its distance from the plane.

• ###### Re: Macro to determine the distance to the furthest point from a plane?

It could be anything - vertex, edge, dome surface... Depends on each specific case.

I am exploring that now. I started with VBA just 5 days ago, so it's all a bit complicated. I don't exactly know how to do what you described./

P.S. By the way, what about GetDepth method? Can it be used for this? I can't find any examples of it's proper usage.

• ###### Re: Macro to determine the distance to the furthest point from a plane?

Try this macro.

• ###### Re: Macro to determine the distance to the furthest point from a plane?

Thank you very much, Matthew. It works. All in all, this is a very complicated macro, far beyond my understanding of how exactly it works. Just out of interest, is this the simplest way to accomplish the task of measuring that distance?

• ###### Re: Macro to determine the distance to the furthest point from a plane?

Its the proper way to get an accurate result.  Go through it section by section and use the API documentation to understand what is happening.

1. Get the selected plane from the selection manager

2. Find a point on the plane and a normal to the plane using API math functions

3. loop through all the bodies in the part using and find the extreme distance along the plane normal vector (and reverse if it) to find the largest distances

4. create planes parallel to the selected reference plane at those distances.

This is pretty typical API stuff.  It an be a bit of a pain at times to deal with the details (i.e. safe arrays to make points and vectors) but you'll get the hang of it.

• ###### Re: Macro to determine the distance to the furthest point from a plane?

Thank you so much. I understand part of it, but most of the API used is a mystery to me, as well as the vector logic. I will get the hang of it. I already modified it to get the functionality I need. Is it something you wrote for this specific challenge, or did you create it sometime in the past?

• ###### Re: Macro to determine the distance to the furthest point from a plane?

I wrote it a while back and use it frequently in my work.  I've since converted it and several other tools into a full blown Add-in.

• ###### Re: Macro to determine the distance to the furthest point from a plane?

I see. It seems to be working pretty reliably. In some very rare cases it provides wrong numbers (I ran a study on my model, and in 1990 iterations, there were 2 obviously wrong values provided by this macro), but I can't seem to track it down for debugging. I will inform you if I manage to do that.

• ###### Re: Macro to determine the distance to the furthest point from a plane?

Alice Caspari wrote:

I don't know if you could tweak this to be what you need but here's a macro that draws a box at the furthest points of geometry.

Note that this is approximate... From the Help on GetBox:

IMPORTANT: The values returned are approximate and should not be used for comparison
or calculation purposes. Furthermore, the bounding box may vary after rebuilding the model.The resulting box encloses the object, but it might not be the tightest box.

The X, Y, Z points returned by SOLIDWORKS are the lower- and upper-diagonal corners that bound the component with the box sides parallel to the X, Y and Z axes. SOLIDWORKS returns box dimensions that enclose the object and are typically close to the minimum possible size.