Command to get the length of an open segment (line, spline, etc...)?

Hello

I'm looking to recover the length of a line and a spline (see the two combined).

 

I came across this link which contains what I need (at least I'm sure of it), but I don't know what exactly all these commands (Length, ArcLength, etc ...) correspond to and what to select (entities) to get the right length: 

http://help.solidworks.com/2016/english/api/sldworksapi/Measure_Selected_Entities_Example_VBNET.htm

 

 

Hello

Try the trajectory length dimension (in "smart dimensions")

I'm looking for the right "macro" that could allow me to recover the length

Hello

Do you select your front line by hand?

It's rather this link that you need for VBA (rather than VB.NET):

http://help.solidworks.com/2015/English/api/sldworksapi/Measure_Selected_Entities_Example_VB.htm

 

I'm using SOLIDWORKS Visual Studio Tools for Application.

It seems to me that it is in VB.net.

Apart from the syntax which changes a little, the link I gave or the one that refers to the VBA, offers exactly the same thing.

After a few tries I can't get any value other than "-1" which means that I don't measure "anything".

@ PL. I don't select anything by hand. I create a line or a spline, I want to select the entity and measure its length, all without going through the interface.

 

Attached is a macro to construct a line and measure its length (with the result -1)

 


exemple.txt

Yes, if you use Visual Studio it's vb.net.

Is your line selected on the screen before starting the measurement?

Have you tried the VBA measurement instruction in SolidWorks by selecting a hand-created line? Does it work?

@ .PL,

I'm not sure I'll follow you: why look on the screen if it's selected the line when I'm scripting the macro? And I tried the instruction in VB.net not VBA (but it's the same thing so I won't do it in VBA).

Anyway, when I try to save the macro to measure anything, there is no trace of a function for measurement, just the entity selection command.

In fact, I have a doubt about what to select (with the SelectByID2 function) to measure a line, a spline, or retrieve the coordinates of a point?  For the moment the only result I have is "-1"

Hello 

I have a simple question, using the measurement tool with the "Ctrl" key gives  a total length of the sketch. Is this method not suitable? Basic, simple  solution that gives results.

may the force be with you.


lg.jpg
3 Likes

Yes, I was asking the question in case you were testing in VBA and not VB.NET.

Otherwise, a link to an example:

https://forum.solidworks.com/thread/107636

See also:

https://forum.solidworks.com/thread/34496

https://forum.solidworks.com/thread/63630

Or maybe you should use this GetLength3 statement:

http://help.solidworks.com/2012/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.ICurve~GetLength3.html

 

@ OBI WAN

Yes, with the measurement tool I can visually retrieve the lengths, distances, coordinates I want on the screen. But I want to do some calculations behind it to distribute points on the trajectory evenly and all this automatically. So I need the vba/vbnet functions to do it

 

@ .PL

I solved the problem with the functions found in the solidworks API help (first link I gave for the vbnet and the one you indicated for the vba). To be able to measure the entities I had to get out of the sketch with the command:

swDoc.SketchManager.Insert3DSketch(True)

For the rest I selected the right entity and used the functions/commands found in the API help:

Example for the measurement of a line, spline and then the two entities:

        ' Measure line length
        boolstatus = swDoc.Extension.SelectByID2("Line1@Esquisse3D1", "EXTSKETCHSEGMENT", 0.0, 0.0, 0.0, False, 0, Nothing, 0)
        swMeasure = swDoc.Extension.CreateMeasure
        swMeasure.ArcOption = 0
        boolstatus = swMeasure.Calculate(Nothing)
        Ll = swMeasure.Length
        Debug.Print("line length=" & ll * 1000 & " mm")
        swDoc.SketchManager.Insert3DSketch(True)
        swDoc.ClearSelection2(True)

       ' Measurement length spline 1
        boolstatus = swDoc.Extension.SelectByID2("Spline1@Esquisse3D1", "EXTSKETCHSEGMENT", 0.0, 0.0, 0.0, False, 0, Nothing, 0)
        swMeasure = swDoc.Extension.CreateMeasure
        swMeasure.ArcOption = 0
        boolstatus = swMeasure.Calculate(Nothing)
        LS1 = swMeasure.ArcLength
        Debug.Print("spline length1=" & LS1 * 1000 & " mm")
        swDoc.SketchManager.Insert3DSketch(True)
        swDoc.ClearSelection2(True)

        • Measurement of the total length of the neutral mesh fiber
        boolstatus = swDoc.Extension.SelectByID2("Line1@Esquisse3D1", "EXTSKETCHSEGMENT", 0.0, 0.0, 0.0, False, 0, Nothing, 0)
        boolstatus = swDoc.Extension.SelectByID2("Spline1@Esquisse3D1", "EXTSKETCHSEGMENT", 0.0, 0.0, 0.0, True, 0, Nothing, 0)
        swMeasure = swDoc.Extension.CreateMeasure
        swMeasure.ArcOption = 0
        boolstatus = swMeasure.Calculate(Nothing)
        Lt = swMeasure.TotalLength
        Debug.Print("Length Lt=" & Lt * 1000 & " mm")

 

Thank you for your help