MisaelGaray MisaelGaray - 1 month ago 4
C# Question

How to wait until an Acad's instance runs to create e new documents?

I'm using objectARX and trying to create a new document. What i'm doing first is to run AutoCad.

Process acadApp = new Process();
acadApp.StartInfo.FileName = "C:/Program Files/Autodesk/AutoCAD 2015/acad.exe";
acadApp.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
acadApp.Start();


Then the problem is when I wait until the instance of Acad is ready. I can't get The process by his name with the Process class because Autocad window is not ready yet and I can't create the AcadApplication instance. It only works when Autocad is completely loaded so I use .

bool checkInstance = true;
//This piece of pure shit listen for an Acad instnce until this is opened
while (checkInstance)
{
try
{
var checkinstance = Marshal.GetActiveObject("AutoCAD.Application");
checkInstance = false;
}
catch (Exception ex)
{

}
}
//Once the acad instance is opende The show starts
Thread.Sleep(12000);
Thread jili2 = new Thread(new ThreadStart(() => acadG.AcadGrid(Convert.ToInt32(grid.floorHeight), Convert.ToInt32(grid.floorWidth), grid.numFloors)));
jili2.Start();
// MessageBox.Show("I don't know why it was executed");
}


The acadGrid Method running in the thread creates a new document in AutoCad and then draws a grid. It sometimes works and sometimes not, and when it works it uses even 50% of CPU. Sometimes i gett this exception.
enter image description here

Answer

I believe the best method would be to create a script (.scr) file that you'll define as a parameter to launching your process, as opposed to trying to wait on AutoCAD to load prior to running your routine.

// Build parameters.
StringBuilder param = new StringBuilder();
string exportScript = @"C:\script.scr";
if (!string.IsNullOrWhiteSpace(exportScript))
{ param.AppendFormat(" /s \"{0}\"", exportScript); }

// Create Process & set the parameters.
Process acadProcess = new Process();
acadProcess.StartInfo.FileName = AcadExePath;
acadProcess.StartInfo.Arguments = param.ToString();
acadProcess.Start();

A script file is a basic text file that lists AutoCAD commands, and any relevant values if you define them, and runs them when loaded. Loading a script as a parameter to your process will automatically make that script run.

Here is a brief guide on creating scripts - http://www.ellenfinkelstein.com/acadblog/tutorial-automate-tasks-with-a-script-file/

You can also do this process using the AutoCAD Core Console. It's a version of AutoCAD included in versions 2013+ that runs exclusively in command line if you want to speed up the process - http://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-console.html

Comments