Coding Macros for Polylines Total Length

Coding Macros for Polylines Total Length

Anonymous
Not applicable
5,914 Views
23 Replies
Message 1 of 24

Coding Macros for Polylines Total Length

Anonymous
Not applicable

Hi there! i'm wreking my brains out trying to code a macros to return the total length for all my similar polylines  on my drawing. Now, from what i've seen coding relies on already in place commands used by Autocad LT (i'm using 2016), but there is no such command that returns to you the length value of a specific polyline, in the command bar. (i know , the length of a polyline can be found in the proprieties table, but i cannot tie with my code to that value, as it needs to be a command to return the specific value).

Please correct me if i'm doing/saying somehing wrong! Any advice would be much appreciated!

0 Likes
Accepted solutions (3)
5,915 Views
23 Replies
Replies (23)
Message 3 of 24

Anonymous
Not applicable

Thanks for your reply! But i;ve already been over every alternative you specified...and no...not good. I'll continue my journey in finding out why Autodesk couldn't create a ''Length'' command.

0 Likes
Message 4 of 24

pendean
Community Legend
Community Legend
0 Likes
Message 5 of 24

Anonymous
Not applicable

Cheers! Already did it! Hopefully will get a response back... Maybe a phonecall will clear things up a bit. 

0 Likes
Message 6 of 24

RobDraw
Mentor
Mentor

@Anonymous wrote:

Hopefully will get a response back... Maybe a phonecall will clear things up a bit. 


 

I wouldn't count on it.


Rob

Drafting is a breeze and Revit doesn't always work the way you think it should.
Message 7 of 24

steven-g
Mentor
Mentor

That kind of automation is not possible in LT, but there is nothing stopping you using the list command on your selected polylines and copy the results from the command history into excel. And in excel filter the resulting list to just show the lengths of all the polylines, (takes about 30 seconds to do it).

But if you want to try creating a macro you could look at using the lengthen command with the percentage option (use 100%) this updates the perimeter variable to read the length of the selected object, and you can then read that variable using diesel. It's doubtful this will help but it is a way to read length information from objects if you enjoy a challenge.

 

0 Likes
Message 8 of 24

Anonymous
Not applicable

Thanks a lot steven-g !!! I was familiar with the excel sheet way for getting the lengts . I kinda tried every option, when it comes to gettting the total length of multiple polylines, and i'm not talking about 2 or 3 polys but hundreds to thousands, and here is where the excel sheet is a bit of a waste of time and hoping that you don't miss a value.....and not to talk of adding every length for a few hundreds of polys :)).

Long story short, not trying to give a hard time to anyone, but trying to get to do things faster & better, and maybe help others to not waste loads of time in theyr work.

 

My chances are narrowing here,  as Macros can't select multiple objects in Autocad LT.....but the only exception is the command ''SELECT''......i've put down a couple of line of code trying to work together ''LENGTHEN'' and ''SELECT'', but just can;t get my head around on how to return the total length value . A bit of advice would be great!

0 Likes
Message 9 of 24

pendean
Community Legend
Community Legend
One more time....

"...That kind of automation is not possible in LT..."

HTH

0 Likes
Message 10 of 24

Anonymous
Not applicable

Slowly starting to realise this....kinda sucks, to have just this one command for which i get no digit value return in my code....

0 Likes
Message 11 of 24

steven-g
Mentor
Mentor
Accepted solution

Indeed that level of automation is not possible. But should not be possible to miss lengths when you are using the list command and excel. Sometimes copying the command history from Autocad can be a problem as it only holds a limited amount of information. If you turn on "logfiles" and find where Autocad stores those logfiles then that keeps a complete record of everything you do within an Autocad session so it is easy to find the results of the list command and copy that. Look in the Autocad "options" under files tab for where the files are stored or set your own location for logfiles. A unique file is created for every drawing you have open so they are easy to find, plus it is just a text file so once you find the results for the "list" command you can just do a simple copy (or even make a copy of the logfile and change the extension to xls which you can open in excel). In excel select the column of data and first "trim" all the spaces away, then use data to columns and finally filter the column that contains the length keyword. One of your other columns will have all the values which you can easily select. It takes a little while to get used to but once you figure out the sequence and how to just get at the data you need then this does only take less than a minute, whether you have 10 polylines or 100,000.

0 Likes
Message 12 of 24

Anonymous
Not applicable

Best reply ever! I will give it a try straight away and let you know. I appreciate your effort to explain the whole process! Happy days!!!

0 Likes
Message 13 of 24

Johneng
Collaborator
Collaborator

The link below is a short cut from Dean's second link on Friday. Skip down to messages 14 and 15 from Paul_Gander he posted a couple of nice excel files that add up the total lengths of lines and polylines. I only looked at the one named Total length (v2.0) from message 15. Also messages 16 and 18 in the link have tips from Steven-g and hwalker that might be of use to you. It would have been nice if Steven-g had pointed out to you this existing excel file for you to use.

 

https://forums.autodesk.com/t5/autocad-lt-forum/how-to-measure-total-length/td-p/6317587

0 Likes
Message 14 of 24

Paul_Gander
Advocate
Advocate
Accepted solution

@steven-g wrote:

 

But if you want to try creating a macro you could look at using the lengthen command with the percentage option (use 100%) this updates the perimeter variable to read the length of the selected object, and you can then read that variable using diesel. It's doubtful this will help but it is a way to read length information from objects if you enjoy a challenge.

 


Thankyou Steven, I had no idea that value was stored in a variable. I wish I had known earlier.

Challenge accepted! Smiley Happy Is this what you had in mind? (Tested on LT2013)

 

*^C^C$m=$(if,$(or,$(eq,$(getenv,pgm_c),0),$(eq,$(getenv,pgm_c),)),_select;"""$m=$(if,$(getvar,cmdactive),\)"""$(if,$(and,$(getvar,menuecho),5),,^p)menuecho;3;$(if,$(and,$(getvar,menuecho),4),^p)_setenv;pgm_c;5)$(if,$(eq,$(getenv,pgm_c),5),_setenv;pgm_pickfirst;$(getvar,pickfirst);;pgm_selectsimilarmode;$(getvar,selectsimilarmode);;pgm_expert;$(getvar,expert);;pgm_clayer;$(getvar,clayer);;pgm_c;10)$(if,$(eq,$(getenv,pgm_c),10),"_setenv;pgm_c;0;_expert;5;_-block;@selset@;0,0;previous;;oops;;_-bedit;@selset@;_zoom;_extents;_-layer;_thaw;`@selset`@;_unlock;`@selset`@;_make;@selset@;;_setenv;pgm_c;15")$(if,$(eq,$(getenv,pgm_c),15),_setenv;pgm_c;0;_selectsimilarmode;0;_pickfirst;1;setenv;pgm_totlen;0;;pgm_c;20)$(if,$(eq,$(getenv,pgm_c),20),"_setenv;pgm_c;0;_line;_none;0,0;_none;10,0;;_selectsimilar;last;;_chprop;_layer;@selset@;;_erase;_last;;_setenv;pgm_c;21")$(if,$(eq,$(getenv,pgm_c),21),"_setenv;pgm_c;0;_pline;_none;0,0;_none;10,0;;_selectsimilar;last;;_chprop;_layer;@selset@;;_erase;_last;;_setenv;pgm_c;22")$(if,$(eq,$(getenv,pgm_c),22),"_setenv;pgm_c;0;_3dpoly;_none;0,0;_none;10,0;;_selectsimilar;last;;_chprop;_layer;@selset@;;_erase;_last;;_setenv;pgm_c;23")$(if,$(eq,$(getenv,pgm_c),23),"_setenv;pgm_c;0;^r_spline;_none;0,0;_none;10,0;;_selectsimilar;last;;_chprop;_layer;@selset@;;_erase;_last;;_setenv;pgm_c;24")$(if,$(eq,$(getenv,pgm_c),24),"_setenv;pgm_c;0;_circle;_none;5,5;5;_selectsimilar;last;;_chprop;_layer;@selset@;;_erase;_last;;_setenv;pgm_c;25")$(if,$(eq,$(getenv,pgm_c),25),"_setenv;pgm_c;0;_arc;_none;0,0;_none;5,5;_none;10,0;_selectsimilar;last;;_chprop;_layer;@selset@;;_erase;_last;;_setenv;pgm_c;26")$(if,$(eq,$(getenv,pgm_c),26),"_setenv;pgm_c;0;_ellipse;_none;0,5;_none;10,5;_none;5,10;_selectsimilar;last;;_chprop;_layer;@selset@;;_erase;_last;;_setenv;pgm_c;30")$(if,$(eq,$(getenv,pgm_c),30),_setenv;pgm_c;0;_selectsimilarmode;$(getenv,pgm_selectsimilarmode);_pickfirst;$(getenv,pgm_pickfirst);_-layer;_off;*;_on;`@selset`@;;expert;$(getenv,pgm_expert);_setenv;pgm_c;loop)$(if,$(eq,$(getenv,pgm_c),loop),_select;_single;_last;"""$m=$(if,$(getvar,cmdactive),^c_setenv;pgm_c;0;_bsave;_bclose;_layerp;;;_-purge;_blocks;`@selset`@;_no;;_layers;`@selset`@;_no;_menuecho;0;_getenv;pgm_msg;$,_setenv;pgm_c;100)""")$(if,$(eq,$(getenv,pgm_c),100),_setenv;pgm_c;0;_lengthen;last;_percent;100;;_erase;_last;;"""$m=_setenv;pgm_totlen;$(+,$(getenv,pgm_totlen),$(getvar,perimeter));;pgm_c;110""")$(if,$(eq,$(getenv,pgm_c),110),_setenv;pgm_msg;""""""""""Total length: $(rtos,$(getenv,pgm_totlen))"""""""""";;pgm_c;loop)

@Anonymous 

This macro should do what you are looking for:

Simply select the linear objects (lines, polylines, 3dpolylines, splines, arcs, circles or ellipses) you wish to add up the lengths of and the macro will loop through them and give you the total at the end. The macro filters out any non-linear objects such as text, dimensions, hatches etc. in order to avoid it crashing when it uses the lengthen command to set the perimeter variable. However, I recommend you set up filters to help you select only the objects you want, such as lines on particular layers or polylines with certain widths and colours, for example. The total is displayed in the format determined by your current unit settings.

 

When pasting the code into the CUI editor, I suggest you open the long string editor in the macro field to make sure it's all copied and check that there are no spaces or returns at the end of the code.

 

@Johneng Thankyou for recommending my spreadsheet. I'm glad you found it useful.

 

Message 15 of 24

pendean
Community Legend
Community Legend
It seems to test well in LT2019 too 🙂
0 Likes
Message 16 of 24

Johneng
Collaborator
Collaborator
Accepted solution

@Paul_GanderThank You for taking the time to create it and your generosity to share it with the rest of us.

0 Likes
Message 17 of 24

Anonymous
Not applicable

OMG, many thanks, can't thank you enough, tested and does the job well, exactly what i wanted. Have a lovely day!!!

0 Likes
Message 18 of 24

steven-g
Mentor
Mentor

Well done Paul, the "challenge" was tongue in cheek, but once more you have proven your skills with macro's really well done, that deserves a gold star.

0 Likes
Message 19 of 24

Anonymous
Not applicable

I know this thread is a bit older, but I had found it a while back and made use of that fabulous macro you had made @Paul_Gander.

The company I work for is far too cheap to spring for AutoCad full, so we're stuck on the license subscription of LT.

With the subscription based version of LT, our software is set to automatically update itself per the outside IT company we use, and with that the version of LT I run updated to 2022.1.

From very early on, your macro worked perfectly - until now.

I don't know what it is about 2022.1 that messed it up, but all it does now is flicker my mouse icon and erase random polylines around the dwg I'm working on, and not even the ones I selected to get the lengths for.

 

Other solutions, such as the LIST to log file to Excel have worked for me in the past, and I've resorted to going back to doing that, but your macro was absolutely perfect, when LT's version was letting it work its magic.

 

Thanks for the ease I got to enjoy for a few years.

0 Likes
Message 20 of 24

steven-g
Mentor
Mentor

Works fine for me, admittedly I don't have the benefit of your drawing which might have some odd objects, but a quick test with just a few polylines in 2022.1 and the macro still performs as well as ever.

Do you get any error messages?

Maybe turn 'on' logfiles' and post a copy of the results here so we can see where the macro might be tripping up. A copy of a dwg file showing which items you select would also help us try and pinpoint the problem.

0 Likes