Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Guidance on writing a script

18 REPLIES 18
SOLVED
Reply
Message 1 of 19
Jeffsg605
759 Views, 18 Replies

Guidance on writing a script

I have a large amount of 2d autocad files in a folder.  I need to find the length of each one.  So far I have been doing each one individually using tlen (http://www.turvill.com/t2/free_stuff/tlen.lsp). Is there a way to write a script that would read in filenames, say from an excel document or text file, open the file, calculate the length of the lines, write that value out to the excel or text file, close the file and continue down the list?  I imagine this has to be possible but I'm not sure how to go about it.  I'm not looking for someone to write the code for me but if anyone has any advice or knows any resources that may help me with this, I would greatly appreciate it.  

Thanks!

18 REPLIES 18
Message 2 of 19
doni49
in reply to: Jeffsg605

1) When I click your link, I get a message that "the file is not found on this server."

 

2) Are you wanting to get the length of EVERY line in each of these files?  What about polylines?

 

If you want to read the list of files from a file, you'll need to use VBA.  A script file will need a hard-coded list.  A lisp routine COULD read the text file -- but the list wouldn't follow through to the next file.



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 3 of 19
Jeffsg605
in reply to: Jeffsg605

1. Sorry, it looks like the end parenthese and period get tacked on at the end.  Hopefully this is better:

http://www.turvill.com/t2/free_stuff/tlen.lsp

 

2. Yes, every line including polylines.  

 

I'm a decent programmer but I've only used VBA a little bit.  I assume this requires the Autocad VBA Enabler?  I will look into it more and ask more specific questions as they come up.  Thanks.

Message 4 of 19
Gary_J_Orr
in reply to: Jeffsg605


@Jeffsg605 wrote:

I have a large amount of 2d autocad files in a folder.  I need to find the length of each one.  So far I have been doing each one individually using tlen (http://www.turvill.com/t2/free_stuff/tlen.lsp). Is there a way to write a script that would read in filenames, say from an excel document or text file, open the file, calculate the length of the lines, write that value out to the excel or text file, close the file and continue down the list?  I imagine this has to be possible but I'm not sure how to go about it.  I'm not looking for someone to write the code for me but if anyone has any advice or knows any resources that may help me with this, I would greatly appreciate it.  

Thanks!


To address the output portion of the task change a line of code in the routine that you posted to have it produce a comma delimited text file (which can then be opened in Excel to give you totals if you want)...

 

  ;GJO: change original code:
;;;  (alert (strcat "Total length of selected objects is " (rtos tl)))
  ;to this (for logging to a comma delimiten text file):
  (setq logfile (strcat (getvar "DWGPrefix") "LineLengths.txt"))
  (setq resource (open logfile "a"))
  (write-line (strcat (getvar "DWGName") " ," (rtos tl)) resource)
  (close resource)
  (setq logfile nil resource nil)
  ;end modification

 

 

To address the rest: I have been working on a utility that processes a script or lisp routine on all of the files in a given folder (and even the subfolders thereof).
It's working pretty good so far and you're free to see if it will give you the "multi-file processing" part of the task.

Close all sessions of autocad, then launch a new session, load the function, then type in "GJO_FLA"

Pay close attention to the prompts, especially the dialog that prompts you to open the directory that you want to process and provide a log file name. . Feedback is appreciated on the function as it is still a work in progress.,

 

Happy Drafting,

Gary

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 5 of 19
martti.halminen
in reply to: Jeffsg605


@Jeffsg605 wrote:

1. Sorry, it looks like the end parenthese and period get tacked on at the end.  Hopefully this is better:

http://www.turvill.com/t2/free_stuff/tlen.lsp

 

2. Yes, every line including polylines.  

 

I'm a decent programmer but I've only used VBA a little bit.  I assume this requires the Autocad VBA Enabler?  I will look into it more and ask more specific questions as they come up.  Thanks.



On 64-bit AutoCAD using VBA is a pretty bad idea, it will have serious performance effects.

 

You do not necessarily need VBA. The problem is that AutoLISP has separate memory areas for each drawing, so  a single AutoLISP program can't control work on separate drawings without some odd contortions.

So you need some external program to control AutoCAD, but it could be as well C#.NET, C++ or any outside system that can use ActiveX.

- I've done that using Common Lisp, and I've seen mentions in these forums of people using Delphi and even Fortran.

 

--

Message 6 of 19
martti.halminen
in reply to: Jeffsg605


@Jeffsg605 wrote:

I have a large amount of 2d autocad files in a folder.  I need to find the length of each one.  So far I have been doing each one individually using tlen (http://www.turvill.com/t2/free_stuff/tlen.lsp). Is there a way to write a script that would read in filenames, say from an excel document or text file, open the file, calculate the length of the lines, write that value out to the excel or text file, close the file and continue down the list?  I imagine this has to be possible but I'm not sure how to go about it.  I'm not looking for someone to write the code for me but if anyone has any advice or knows any resources that may help me with this, I would greatly appreciate it.  

Thanks!


I haven't tested this, but I think one way of doing this could be creating a separate AutoCAD profile just for this, and have the work done using its acaddoc.lsp file.

Before running that, create a list of all file paths you want to operate on, and write it to a file with a known address.

Open the first file on the list using the special profile.(or even just an empty new drawing, if your calculations won't crash on that)

 

In the acaddoc.lsp:

- close any other drawings currently open [the previous drawing handled]

- do your calculations, and store the results in a file

- remove the current drawing's path from the file of paths to handle

- if the list of pending drawings is empty, summarize the results, else open the next drawing on the list.

 

If you have performance problems, you could try the "headless"  AutoCAD Core Console.

--

 

Message 7 of 19
Jeffsg605
in reply to: Gary_J_Orr

Thanks Gary!  That looks very promising.  I haven't had much time the past couple days to work with it but I definitely intend to soon.  One quick question though, where does the .log file get stored?  In the directory of the cad files asked for in the beginning?  I ran your script with that tlen function I linked to and no file was created.  I've only had a few minutes to mess with it so I may have missed something obvious.  In any case, thank you very much!  I'll let you know how it goes.

Message 8 of 19
Gary_J_Orr
in reply to: Jeffsg605

The log files that my routine creates (success/fail for ability to complete whatever lisp/script is passed to it) goes in the directory that you open (the directory that is to be processed) and with the filename that you specify ...

The modified Tlen function should then generate log files in whatever directory is being processed at the time (the modification that I supplied you with gets the directory of the current file for the path.)

 

Did you modify the Tlen Function per my earlier post?

 

It should look something like the attached...

Gary

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 9 of 19
Jeffsg605
in reply to: Gary_J_Orr

I used your attached code and it works very well.  There is still no log file created though and I followed the prompts carefully.  When it asks for the function name, "(C:TLEN_Log)" appears in the box.  When I retype "TLEN_Log" and hit enter, it is replaced by "(C:TLEN_Log)".  I don't know if this is a problem or not but no new documents are being created, including LineLengths.txt from the lsp routine you attached.  I appreciate your help so far and I will work with it more.  Thanks again!

Message 10 of 19
Gary_J_Orr
in reply to: Jeffsg605

The recursive function that I supplied uses vlisp syntax to submit whatever command is required to the secondary acad session that processes the files, therefore it needs the entire "(C:TLEN_Log) and that is why it changes it.
The The modified TLen function still requires user input to select the linework to process.
Does the secondary session pause for you to select your linework? It did for me in a quick test... Are you selecting the desired linework in the secondary Acad session?
A further rewrite may be required to TLen_Log to remove user input (creating a selection set by whatever values that you want to predefine)

here are the results in the Linelengths.txt file from my test (the files are identical, and therefore so are the reported values):
Drawing1 (2).dwg ,48.2843
Drawing1 (3).dwg ,48.2843
Drawing1.dwg ,48.2843
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 11 of 19
Gary_J_Orr
in reply to: Gary_J_Orr

quick thought: are the files that you want to process read-only?
This program was built to do work in files, therefore it checks to see if they are read-only before attempting to send the command...

My routine would require modification to process for information gathering only (removing any checks against read-only and skipping the saving of the processed files.)
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 12 of 19
Jeffsg605
in reply to: Gary_J_Orr

My files are all .dxf's.  Could that be causing the issue?  I can't seem to open a .dxf using the command "-open PartNumber.dxf".  I get a window that says "Cannot find the specified file".  Maybe nothing is being opened?

Message 13 of 19
Gary_J_Orr
in reply to: Jeffsg605

yeah, that is a bit of a problem... my routine is hardcoded for *.dwg files... therefore it isn't finding any drawing files to process.
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 14 of 19
Jeffsg605
in reply to: Gary_J_Orr

Thats unfortunate for me.  I figure I can use your modified TLEN_Log file and write a script that looks like this:

OPEN
C:\parts\729.dxf
TLEN_Log
all

 

OPEN
C:\parts\1218.dxf
TLEN_Log
all

...

 

It's a little tedious but it sure beats what I was doing before.  I'm thinking I can write a C program to read a list of my files and spit out this script.  I have literally thousands of parts to do.  Thanks a lot for your advice, you've been a big help.  

Message 15 of 19
Gary_J_Orr
in reply to: Jeffsg605

try these...

modified (quick and dirty so I may have missed something) GJO_FLA_RO will prompt for filetype (dwg or dxf) and will process as read-only so there are no issues with filetype when exiting.

Modified TLEN_Log to select everything (in current space when the file is opened, which should be modelspace with a dxf file I believe).

 

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 16 of 19
Jeffsg605
in reply to: Gary_J_Orr

Excellent, that looks really good. I will be back in the office on Wednesday and will give it a shot. I'll let you know how it goes. You have been extremely helpful so far, I really appreciate it.
Message 17 of 19
Jeffsg605
in reply to: Jeffsg605

I ran your programs and this is what happened.  When the TLEN_Log program is ran, I have to hit Ctrl+A to select all for each drawing.  Then it continues on to the next one where I have to do the same thing and so on.  Also, the processed.log file that is created only has the filenames of all the programs.  I really appreciate your help so far and you don't need to keep going out of your way.  I can probably figure it out from here.  However if you do fix it, I will be happy to test it for you.

Thanks!

Message 18 of 19
Gary_J_Orr
in reply to: Jeffsg605

If you are still having to select objects then I have to ask if you are using the most recent version of Tlen_Log that I provided in my last post. That version should select everything without user interaction and should create a file named "linelengths.txt" with the totals that the original version would have given you in each directory that it finds files in.

Double check that you downloaded that most recent version.

the file "processed.log" that the controller function creates should simply contain the directories and file names of everything that it found and opened for processing.
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 19 of 19
Jeffsg605
in reply to: Gary_J_Orr

You, my friend, are a genius!  I used the correct TLEN, I just looked at the wrong file.  Huge help! Thank you so much!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost