Automate opening AutoCAD, run a custom command on multiple files

Automate opening AutoCAD, run a custom command on multiple files

pari_dhanakoti
Enthusiast Enthusiast
1,057 Views
10 Replies
Message 1 of 11

Automate opening AutoCAD, run a custom command on multiple files

pari_dhanakoti
Enthusiast
Enthusiast

Hi,

I am a .Net programmer. Very new to Civil Software. I have gone through some of the tutorials and have completed creating a custom command and executing it in AutoCAD command line. This is all the intro I have.

 

Next up, I have 2 phases.

 

Phase #1:

  1. Make my command to work on multiple files that user picks from a folder.
  2. The command must run on all the files that the user picks.

Phase #2:

Eventually, I would like to automate this whole process - It would like below

 

  1. AutoCAD can be opened automatically,
  2. A specific folder can be open and all files in the folder will be selected
  3. The custom command will be run on each file in the folder and it's output will be saved in a different folder.

 

Following are my questions:

  1. Phase#1 -> Do I have to have a custom file dialog that lets user pick one or more file in a folder? Please hit me up with a sample I can follow or any links I can read up on?
  2. Phase #1 -> Are there alternatives to NETLOAD to load the assembly, like an option to load the add-in when I open Autodesk without having to use NETLOAD? 
    I tried doing it via Startup Suite in Autocad (by using APPLOAD command) but that does not allow .dll files for some reason. Is this expected? (see below) *.dll is not an available option.
    pari_dhanakoti_0-1727806874655.png
    Another option I came across is this. 
    But it talks about using a bundle / PackageContents.xml which I am not sure where to find. Please any links to this is appreciated. 
  3. Phase #2 -> As far as automating the whole thing I came across Autoit. Am I in the right path trying to use this to automate opening of AutoCAD, select a predefined input folder, run the command on each file in the folder and save output to a predefined folder? Please link me to any other automation utilities that are relevant.
0 Likes
1,058 Views
10 Replies
Replies (10)
Message 2 of 11

pari_dhanakoti
Enthusiast
Enthusiast

Posting this for posterity  - As a quick alternative for running manually NETLOAD, I updated the AutoDesk/Support/acad.lsp file to load my plugin. something like below, saved it and restarted Autodesk. Now this loads the plugin automatically.

(defun LoadMyPlugin ()
  (command "NETLOAD" "C:\\Path\\To\\Your\\Plugin.dll")
)
(LoadMyPlugin)

 

0 Likes
Message 3 of 11

norman.yuan
Mentor
Mentor

There are different ways to have the plugin loaded automatically when AutoCAD starts, and yes, using acad.lsp is one of them.

 

Since you are doing batch processing, it is assumed that there is no need for user interaction when the batch work started. Yes, you may want to provide a UI/folder open dialog for user to select a folder and/or specify a destination folder before the batch begins.

 

However, open each drawing file of many in AutoCAD editor to process it is not a good idea, because opening drawing file visually takes much longer, even longer than the data process by your code. 

 

You can consider to open drawing as side database (i.e. read drawing file into a database in memory, but not in AutoCAD editor), and do the needed work (retrieving data from drawing, updating data in the drawing...) in memory/invisible. It is a lot faster. 

 

If the number of drawings is very high, say, dozens of them, with large drawing file size, batch process all of them in single running AutoCAD session would fragment the memory so badly and eventually crash AutoCAD.

 

This is where AutoCAD core console or Autodesk's Platform Service cloud come into play. Not sure how much you know AutoCAD core console, but it is the better option for batch drawing process.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 11

pari_dhanakoti
Enthusiast
Enthusiast

Hi Norman,

Thanks for the detailed response. My first option before moving to AutoCAD API was accoreconsole.exe. I am trying to run NWCOUT on one or more .dwg files that the user is choosing. So I was generating a .scr (Autocad script file) that opens the drawing, executes NWCOUT and closes the file.  I was executing the scr via accoreconsole.exe. I noticed that if I have even 2 drawings each of size 1 MB, which is very small, it was taking a significant 3 mins of time. 

So I was hoping may be running it as a plugin will be faster. But from what you are explaining, I fear that is going to be even more slower than what I am doing with accoreconsole.

 

What you have mentioned about reading the drawing into DB, sounds like a better approach? Would you still consider that is the best option for my usecase, where I need to batch process a bunch of files that user will select OR target a folder of .dwg files to do the NWCOUT on each of them? 

0 Likes
Message 5 of 11

Keith.Brown
Advisor
Advisor

You might want to take a look at ScriptPro by Autodesk.   It is a .Net project that comes with source that will open each dwg that you select and run a .scr file on it.  It might be a quick option for you if you are just running simple commands.  At the least you can examine the code and possibly reuse parts of it.


0 Likes
Message 6 of 11

norman.yuan
Mentor
Mentor
the key to have fast drawing process with core console is to run single instance of core console exe against each drawing. It is really fast from my experience. You may want to create a either small desktop EXE, or a console exe to gather the source/destination folder, plugin to load, command script for each Acad core console. Then in a call Process.Start() to start Acad core console in a loop to go through each drawing. That is, you DO NOT open/close(save) more than one drawing per each Acad core console instance.

You only use desktop/full AutoCAD instance when the batch count is small (or the drawing sizes are small).

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 11

pari_dhanakoti
Enthusiast
Enthusiast

Thank you. I originally started with that idea and have a desktop app that initiates a single instance of accoreconsole.exe for each drawing. My test drawings are 2 MB each, but I felt the nwcout on them by calling Acad core console via Process.Start() was slow. Now I am thinking that is the normal speed?! (It was taking about a min for each file).

 

As an alternative, for someone else looking to do it via an add-in instead of WPF app, this is how I did it: 

This multi selects input files and runs NWCOUT on each of them, saves the file by the dwg's name to an output folder, the user provides. Hope this helps someone who is new to Autodesk API

string outputDirectory = folderBrowserDialog.SelectedPath;

            foreach (var dwgFilePath in dwgFilePaths)
            {               
                using (Database db = new Database(false, true))
                {
                    db.ReadDwgFile(dwgFilePath, FileOpenMode.OpenForReadAndWriteNoShare, true, "");

                    // Switch to the new database
                    using (DocumentLock docLock = doc.LockDocument())
                    {
                        HostApplicationServices.WorkingDatabase = db;

                        // Construct the output file path
                        string outputFilePath = Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(dwgFilePath) + ".nwc");

                        // Temporarily disable FILEDIA
                        object originalFiledia = AutoCADApplication.GetSystemVariable("FILEDIA");
                        AutoCADApplication.SetSystemVariable("FILEDIA", 0);                                             
                        
                        // issue command
                        ed.Command("_NWCOUT", outputFilePath);
                        
                        // Restore FILEDIA to its original value
                        AutoCADApplication.SetSystemVariable("FILEDIA", originalFiledia);

                        // Reset the working database
                        HostApplicationServices.WorkingDatabase = doc.Database;
                    }
                }
            }

 

0 Likes
Message 8 of 11

norman.yuan
Mentor
Mentor

I did not pay attention in your later reply mentioning that you want to run the command "NWCOUT". Since there is no API exposed that does the work as this command does (exporting model to Navisworks), so, you have to call the command with either SendStringToExecute() or Editor.Command(). Because of this, the code you just showed (of using side database) does not make sense at all: in order to run the command the drawing must be active drawing in AutoCAD - that is, you must open the drawing in AutoCAD editor, not as a side database in memory, which has no document/editor associated!

 

I never used "NWCOUT" command, not sure if the process of the command execution depends on the visually accessible editor, thus not sure if Acad core console is suitable for this command or not (but from your description, it seemed working: if it worked in the core console but took long time, I can only imaging it would take even longer when doing it in desktop AutoCAD. But anyway, side database idea will not be an option for running this command.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 9 of 11

pari_dhanakoti
Enthusiast
Enthusiast

Looks like you called it out right. I was naive to assume that Reading DWG File into memory and executing the NWCOUT will be fine. While it generates a .nwc file and it's size is not 0 KB, it seems to match the size of the ones that I generate normally (that is using NavisWorks) but when I open the custom command generated NWC file (that uses the code I showed by loading into memory) it shows nothing. Makes me think, what you suggested is right 😞 

So I am curious, for what kind of commands / operations is it best to read a dwg into Database? 

For NWCOUT is my only option SCR file that can be run with accoreconsole.exe using Process.start() in .Net?

0 Likes
Message 10 of 11

norman.yuan
Mentor
Mentor

Most, if not all, the APIs in the Autodesk.AutoCAD.DatabaseServices contained in the acdbmgd.dll can be used to access drawing data in the drawing database by reading the DWG file into a side database.

 

Since your focus is to exporting data model in the DWG file as Navisworks, and there is no API available to public in the AutoCAD side for doing this work, therefore running command NWCOUT is the only option, thus, the drawing MUST BE Opened in AutoCAD session as Document before you can run the command (either use SendStringToExecute() or Editor.Command()).

 

If using core console indeed creates NWC model file, it should be no slower than doing it with full AutoCAD desktop, I'd guess. Again, I never used NWCOUT command, you need to try it with core console to see if it works or not. In this case, there is no .NET plugin involved: you only need one line of script macro for the NWCOUT command. Once it proves working, then you may write a simple EXE app (.NET, or not, or even a power script tool) to automate the process of batch drawing operation: just start a loop for each drawing - run the core console EXE with a input drawing, input script (for the command NWCOUT) as core console EXE startup arguments.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 11 of 11

Keith.Brown
Advisor
Advisor

See my previous reply about autoscript pro.  It will do exactly what you want.