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

Importing/Converting STEP files to DWGs using LISP

3 REPLIES 3
Reply
Message 1 of 4
jason_aPCJML
319 Views, 3 Replies

Importing/Converting STEP files to DWGs using LISP

I have a few thousand ST(E)Ps that I need to turn into DWGs. I tried writing a script with LISP but I can't seem to get it to actually import anything. I've included the script I am trying to use and a comment on the line that is failing. I've only done very basic programming in LISP so I'm not the best at debugging it.

Is this the best solution for programmatically converting STPs to DWGs? (preferably through ACAD, open to other local conversion tools as well) Any pointers, resources, or help is greatly appreciated.

I originally had the import line as "-Import" But that would result in this error:

 

Cannot invoke (command) from *error* without prior call to (*push-error-using-command*).
Converting (command) calls to (command-s) is recommended.

 

From my understanding the "-" means to call the function with params instead of popping up a dialog, but I don't think this issue is related to the Import command not working/me not understanding how to call it properly.

Here is the script I am trying to run:

 

(defun process-step-files (/ step-files output-dir)
  (setq step-files (vl-directory-files "C:/Steps/" "*.step" 1))
  (setq output-dir "C:/DWGs/") 

  (if (null step-files)
      (prompt "\nNo .stp files found in the specified directory.")
    (progn
      (foreach step-file step-files
        (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
        (setq model-space (vla-get-ModelSpace doc))
	(prompt "\nImporting...\n")
	(prompt (strcat "C:/Steps/" step-file))
        (command-s "IMPORT" (strcat "C:/Steps/" step-file)) ; FAILING ON THIS LINE/NOT MAKING IT PAST THIS LINE
	(prompt "\nStep 1")
        (setq imported-objects (vla-get-ImportedObjects doc))
	(prompt "\nStep 2")
        (setq imported-object (vla-item imported-objects 0))
	(prompt "\nMoving imported object into model-space...")
        (vla-move imported-object '(0 0 0) model-space)
	(prompt "\nSaving...")
        (command-s "-SAVEAS" (strcat output-dir (vl-filename-base step-file) ".dwg") "2018")
      )
      (princ "\nConversion process complete.")
    )
  )
)

(process-step-files)

 

 

 

3 REPLIES 3
Message 2 of 4
pendean
in reply to: jason_aPCJML


@jason_aPCJML wrote:

...I have a few thousand ST(E)Ps that I need to turn into DWGs...


You can always purchase a fix designed for the task https://cadsofttools.com/products/abviewer/tutorials/how-to-convert-3d-model-to-dwg/ 

Message 3 of 4
paullimapa
in reply to: jason_aPCJML

I don't have any step files to try this on but perhaps you can attach a couple of samples?

What happens if you change this line:

 

(command-s "IMPORT" (strcat "C:/Steps/" step-file))

 

To this:

 

(command "_.-IMPORT" (strcat "C:/Steps/" step-file))

 

Also this line fails:

 

(setq imported-objects (vla-get-ImportedObjects doc))

 

Because there's no such function as  vla-get-ImportedObjects

Is there a reason for moving the imported objects when you should be already in Model space?

Now this line of code would save and switch your current drawing to that new drawing file:

 

(command-s "-SAVEAS" (strcat output-dir (vl-filename-base step-file) ".dwg") "2018")

 

Perhaps just use the Save command and give it a different name. Then you won't be in that newly saved file:

 

(command "_.-SAVE" (strcat output-dir (vl-filename-base step-file)))

 

Before the foreach loop finishes to start on the next step file you can erase everything imported like this:

 

(command "_.Erase" "_All" "")

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 4 of 4
Sea-Haven
in reply to: jason_aPCJML

I would go back a step and use a script you can write the script using a lisp file to get step file names.

 

Second suggestion is if step file name has spaces can cause problems as the space is used as a "end of", so may need the step file to be "c:\\step\\this is my file name". You need the import to run without any user input.

 

Something like this. just write down all the steps when you do "import" manually to work correct.

 

New
(setvar ctab 'model)
(command-s  "-import" "C:\\steps\\file 1" there may be more steps here)
(command-s "-SAVEAS" (strcat "C:\\mysteps out"  (vl-filename-base "C:\\steps\\file 1") ".dwg") "2018")
close
New
(setvar ctab 'model)
(command  "-import" "C:\\steps\\file 2" )
(command "-SAVEAS" (strcat "C:\\mysteps out" (vl-filename-base step-file) ".dwg") "2018")
close
New
(setvar ctab 'model)
(command-s  "-import" "C:\\steps\\file 3" .....)
(command-s "-SAVEAS" (strcat "C:\\mysteps out" (vl-filename-base step-file) ".dwg") "2018")
close

 

 

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report