Sweep Length Calculator

Dear Ladies & Gents,

Here I'm pleased to share my super-macro .

I've came across several times, (and not only me, as far as I've seen,) with the need of calculating the total length of 1 or several sweep features, where the path is made of multiple segments, sometimes not easy to select one by one.

So this macro calculates the total length of all the sweep features within a part, distinguishing between construction and non construction segments.

It could be a wire, tube, pipe, cable, etc....

Once calculated, it returns through a MsgBox:

But also added as a Custom Property "Length" to the current configuration, that can be pulled from a BOM.

08-08-2018: macro updated including Composite Curves and any kind of sweept, solid or surfaces.

Enjoy it!!

Message was edited by: Marcos Rodríguez Macro updated now considering CompositeCurves

Message was edited by: Marcos Rodríguez Added an Alternative macro, with the Pre-Selection option by Ali request.

After a full review of this Macro, I found several things that could go wrong, share sketches, suppresed features, modification after the sweep, etc... and to be honest there were too many requirements to meet for this pour macro. 

Usign it intensively for calculating fibre optics length, I've got to the conclusion that the process could be simplified a lot based in Volume. So the length is calculated as the Volumen divided by the cross section.

The few restrictions are:

  • the cross section must remain constant,
  • and all lengths must come from solid bodies.

So the tricky part is to determine the cross section. In this case it looks for a sweep features and check its faces, till it determines which one is the cross section (last one).

It can calculate the lentgh of something like this, as long as at least 1 feature had been made with sweep. other could be patterns or extrude.

It works as well if a representative face of the cross section is selected previous running the macro.

Enjoy Macro7! Simple and easy.

SolidworksApi macros
Answers 38
SF
SF 2018-08-01 Best Answer

Unfortunately it doesn't work when the path is a comosite curve which in my case it's most of the time.

Here's an example

I receive an error on this line:
Set swSweepPathSk = swSweepFt.GetSpecificFeature2

MF
MF 2018-08-01

Hi Habid,

Thanks for the review. I'll add some lines of code to include the composite curves as well.

Composite curves returns the total length using the measurement tool, that's why I didn't consider them.

But it may happen that different sweep features are made with composites curves but also with Sketches.

I'll let you know when it's ready.

Cheers.

SF
SF 2018-08-01

thanks for your efforts.

MF
MF 2018-08-08

Hi Habid,

I've got a moment of inspiration and I've updated the macro.

Could you check it if it works fine now, please, and let me know.

You'll need to download it again.

Cheers.

SF
SF 2018-08-08

it’s 1AM here in Tokyo. I’ll check it tomorrow morning as soon as I’m at my desk.

Thanks for letting me know.

SF
SF 2018-08-09

Million thanks.
It works perfectly.

MF
MF 2018-08-09

Hi Michael Fernando,

Thanks for the link. Actually I had already seen that and it's very interesting. I've learnt new things like the face curve feature. This process could be even simplify a little bit using only 2 faces curves, and then the middle curve of the surface resulting from blending the initial curves.

The main difference is that this process can take at least 20min to 30min to set it up. Also that type of bodies, that are very unlikely to find, maybe some import geometry.

What I suggest is a macro, that takes seconds to determine the total length, of sweep features. These features are the most common features to make tubes, wires, cables, etc.... It has even some advantages, like it can work in multibodies, surface & solid, as long as they're sweep features.

I hope somebody else find it useful.

Cheers.

RB
RB 2018-09-27

Good morning Marcos,

This macro works really well. Thank you for creating and sharing this awesome program!

I have a little problem. On the BOM where it shows the length, it is in 6 decimal places.

Is there a area in the program where I can change it to 2 decimal places?

I appreciate all your help.

Robbie

AV
AV 2018-09-27

Thanks for writing the macro. It works well.

May I suggest an enhancement? Give the user the option to pre-select the swept features to be measured, in case it has multiple sweeps in the model, and needs to measure only some of them.

MF
MF 2018-09-27

Hi Robert,

Thanks for the acknowledge.

I think you can change the  decimal places either in part or drawing environment, from Options->Document properties->Units

Regards

comment imagecomment image
MF
MF 2018-09-27

Hi Alin,

Thanks for the review. Well definitely it's a good enhacement, I'll take a look when I have got a sec. and will get back.

Regards.

AV
AV 2018-09-27
<p>Marcos Rodríguez wrote:</p><p></p><p>Hi Alin,</p><p style="min-height: 8pt; padding: 0px;"></p><p>Thanks for the review. Well definitely it's a good enhacement, I'll take a look when I have got a sec. and will get back.</p><p style="min-height: 8pt; padding: 0px;"></p><p>Regards.</p>

Cool! So, if nothing is pre-selected, return the total length of all sweeps. Otherwise, return the total length of the pre-selected sweeps.

RB
RB 2018-09-27

Hi Marcos,

Thanks for the quick response. I tried exactly what you suggested and it didn't work.

Any other ideas?

Robbie

DG
DG 2018-09-27

Can this be made to work in assembly?

AV
AV 2018-09-27
<p>Deepak Gupta wrote:</p><p></p><p>Can this be made to work in assembly?</p>

Excellent idea!

MF
MF 2018-09-27

Hi Alin,

I was taking a quick look to that macro, and I've found a little problem.

When the paths of each sweep are on same sketch, it counts the lenght of all segments.

So in this case the total length should have been 3 times less.

comment imagecomment image
RB
RB 2018-10-23

Hi Marcos,

My computer got swipe for several important reasons and when I try to download the macro again and it will not calculate.

This error shows up. 

The Box pops up and says : Run time error '13": Type mismatch

                                                  Set swApp = Application.SldWorks

                                                  Set swPart = swApp.ActiveDoc

                                                  Set swSelMgr = swPart.SelectionManager

                                                  Set swFeatureMgr = swPart.FeatureManager

The bold letters represents the yellow highlight where the arrow was pointing to from Visual Basic Application.

This program was just working fine before and I hope you can provide a solution for this because i really enjoying using this.

Have a great day!

Robbie

RB
RB 2018-10-23

Hi Marcos,

My computer got swipe for several important reasons and when I try to download the macro again and it will not calculate.

This error shows up.

The Box pops up and says : Run time error '13": Type mismatch

                                                  Set swApp = Application.SldWorks

                                                  Set swPart = swApp.ActiveDoc

                                                  Set swSelMgr = swPart.SelectionManager

                                                  Set swFeatureMgr = swPart.FeatureManager

The bold letters represents the yellow highlight where the arrow was pointing to from Visual Basic Application.

This program was just working fine before and I hope you can provide a solution for this because i really enjoying using this.

Have a great day!

Robbie

MF
MF 2018-10-23

Hi Robert,

Thanks for the acknowledge, I really appreciate it.

I've download the macro, and ran it, and it works fine to me. Have you tried it in a different computer??

If Macro stops in there-> Set swPart = swApp.ActiveDoc it may be happening the following things:

  1. There is not a part file opened. You're running the macro either from an assembly, or drawing. I should have put some code to correct this, but also I assume people knows what are doing.
  2. The macro is not loading the SolidWorks Libraries.

make sure it maps the location of the following libraries, from Tools->References.

Also bear in mind, that this macro has some limitations, (read the full post) that could give you more lenght that it really is. I was working on that before my live got complicated.

If you provide an e-mail, I could send you the macro file separetely.

Regards.

comment imagecomment image
RB
RB 2018-10-23

Hi Marcos,

Everything is OK now! It goes to show how one little mental screw up messes everything else.

I apologize for the extra work you had to go thru today.

As far as the reducing the number of digits down to two digits, that would be awesome!

Please let me know when you do. Thanks again. Have a great day!.

Robbie

MF
MF 2018-10-23

Hi Robert,

Not a problem, you're welcome.

You can introduce this line of code by the end of the macro, so you've got 2 decimals, let me know if you don't know how to do it.

TotalLength = Int(TotalLength * 100) / 100

Or you could ask first how many decimals through an input box, dec = inputbox("Decimals?")

TotalLength = Int(TotalLength * 10^dec) / 10^dec

Enjoy it!

comment imagecomment image
RB
RB 2018-10-24

Good morning Marcos,

Once again you came to the rescue! You made my day! The measurement in the program comes in two decimal places.

The length shows up on the BOM in 6 decimal places and I'm going to try figure out how to get it down to 2 decimal places.

Til next time.

Robbie

MF
MF 2018-10-24

Hi Robert,

I wrote you separately a msg, since the topic has drifted away a little bit.

Regards.

DG
DG 2018-10-24

Is it just me, or is it a little strange that this isn't already a feature in solidworks Evaluate tab?

MF
MF 2018-10-24

Hi Dan,

Obviously you can use the measure tape from the Evaluate Tab, but sometimes you may have many small segment sketches, or many sweep features, and it can take a while.

Macro measures in less than 1 sec. and returns the value into a property.

Oki?

DG
DG 2018-10-24

No, I get it.  All the more reason to question why a productivity tool like this isn't already native?

MF
MF 2018-10-24

Well actually there is such a tool, but it works only with the 3D Electrical. It justifies that tool, because all the cables are sweep features, and they can be splines.

Probably in another routing environment, piping, weldments, ....

Provide that feature in a basic interface, it'd complicate the Evaluation Tab with marginal tools.

MF
MF 2018-11-08

Hi Deepak,

Sweeps only exits in Parts, Do you mean run in an assembly traversing through all parts and detect if they've got sweeps?? and Finally returning the total lenght??

Regards.

MF
MF 2018-11-08

Ali, macro with preselection option has been added.

However there are limitations due to the API for accesing sweep paths is not fully deployed: Limitations are the following:

  1. Paths made of Sketches with several contours.
    • Access to the Group of segments is not granted. So there is no way to determine the geometry of path
  2. Paths made of more than 1 Edge
    • Access to the Group of Edges is not granted. So there is no way to calculate the length of path
  3. Hybrids Paths made of sketches and Edges.
    • Only accesible to 1 of the sketches. Even though there was 3 sketches making up the Path.

Hopefully 2019 will have fixed these limitations.

SH
SH 2019-01-29

Hi Marcos

This macro is great and very useful, thanks for sharing and supporting. Unfortunately, I'm one of those that doesn't know how to properly insert that decimal code into the macro. I've added into the same place but it comes back with a compile error. Any guidance?

Thanks

Stuart

SL
SL 2019-05-08

Fantastic macro Marcos! I've not written macros before but was able to figure out how to convert the result into feet. Down near the bottom of the routine I changed 2 lines of the code. Seems to work.

      End If
        TotalLength = TotalLength + Length * 3.2808398950131E-03
    End If

MsgBox "Total Lenght = " & TotalLength & "ft"

End Sub

MF
MF 2019-05-09

Hi Scott,

Thanks for the acknowledge!!!.

Make sure your conversion factor multiplies everything, to do so, you might include brackets:

TotalLength = ( TotalLength + Length ) * 3.2808398950131E-03
   

RJ
RJ 2020-01-24

Hi there,

Great tool and a shame that there is a need for these kinds of macro's I'm fairly new to this whole macro thing hence my question. 

I've gotten the decimals right, in the message box and in the BOM. I've added a custom table-row to my drawing displaying the total length of pneumatic tubing. 

Would it be possible for this macro to add the length of the individual tubing. As can be seen in the cutlist we've listed the individual lengths, this is now updated manually. Would it be possible for this to be automated? 

hope to hear from you? 

Robin

comment imagecomment image
MF
MF 2020-02-25

Hi Robin,

Thanks for the acknowledge.

If each tube is made with only 1 sweep it could be easy to relate the sweep with the cut list, otherwise it seems more complicated.

I'll take a look if I've got time, no much unfortunately.

Regards.

SF
SF 2020-04-28

Marcos-

This looks like a great macro. Where should I save the macro to so that it always runs when I open up solidworks? I need something that would auto-generate in the BOM but I do not know where to run it from. Sorry new to this

MF
MF 2020-05-04

Hi Michael,

Thanks for using the macro.

You can save it anywhere. Most people have a specific folder for Macros, where all the settings & SW libraries are. 

You'll need to configure the macros folder, from Options->File location.

You can also assign a button to the macro. Search in YouTube/ or SW  doc.

Regards.

HC
HC 2022-02-25

Hi Marcos, 

Thank you for the macro! So far it works to measure to sweep (good for the tubes I have drawn!), but I can't see the length in the BoM/custom properties of the part. What do I do wrong?

Thank you in advance!


Regards,


Hneri