.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

pausing doc.SendStringToExecute() until the command has finished

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
bryan.thatcherPGLDM
343 Views, 5 Replies

pausing doc.SendStringToExecute() until the command has finished

I'm collecting the DWG files in a directory, opening each one and running several commands, saving and closing the drawing with the code below. 

 

What's happing is its opening each drawing and creating the circle in succession, then saving and closing each one. How can I wait for Acad to save and close the drawing before opening the next? Thanks. 

 

		private void GetDWGs(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] Files = di.GetFiles("*.dwg");

            foreach (FileInfo file in Files)
            {
			OpenDrawing(file.FullName);
            }            
        }
			
		
		public void OpenDrawing(string DWGpath)
        {      

            DocumentCollection acDocMgr = Application.DocumentManager;

            if (File.Exists(DWGpath))
            {
				acDocMgr.Open(DWGpath, false);
                doc = acDocMgr.MdiActiveDocument;
                Database database = doc.Database;                            

                doc.SendStringToExecute("._circle 2,2,0 4 ", true, false, false);
                doc.SendStringToExecute("_QSAVE ", true, false, false);
                doc.SendStringToExecute("_CLOSE ", true, false, false);
            }
            else
            {
                acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + DWGpath + " does not exist.");
            }         
        }
5 REPLIES 5
Message 2 of 6

Using SendStringToExecute() to do what you're seeking to do essentially amounts to a horror show. It doesn't execute the commands you pass it until your code exits and returns control to AutoCAD, so it is not a solution to this (or most, for that matter) problem.

 

The example code below requires the ContextManager.cs file that is attached.  It was designed from the ground up to support batch processing of DWG files without the limitations imposed by code that runs exclusively in the application context (where your code is running). I modified it from the original example code (from this post), to do what your code is attempting to do (draw a circle and save the file).

 

If you add the ContextManager.cs file to your project, and replace the button4_Click handler in that file with the one shown below, and run it from the palette set created and shown by the example code (shown by using the DCPALETTE command), it should do exactly what the code you posted intended to do.

 

I haven't tested ContextManager.cs on recent AutoCAD releases, but since AutoCAD and it's managed API have been stagnant for so long, I don't see any reason why it wouldn't work on any release prior to AutoCAD 2024 (which it probably won't work on as-is). If you decide to try it and it doesn't build, let me know what the issue(s) are.

 

 

 

/// <summary>
/// The click event for button4 demonstrates how to use
/// the second overload of the ForEach() extension method 
/// to batch-process multiple DWG files selected from a 
/// file dialog.
/// </summary>
/// <remarks>
/// Each file in the argument list is sequentially opened
/// and the delegate invoked with the Document representing
/// the open file. When the delegate returns, the document
/// is closed, and processing proceeds to the next file.
/// 
/// The process performed on each file:
/// 
///   1. Draw a circle at 2,2,0 with a radius of 4.0 units
///   3. Save the file
///   
/// The delegate that performs the processing of each file
/// executes in the document context, allowing the use of
/// the Command() method. The delegate must save the file
/// if necessary, as it will not be saved automatically,
/// and any changes made by the delegate will be discarded.
/// </remarks>


void button4_Click(object sender, EventArgs e)
{
   OpenFileDialog ofd = new OpenFileDialog();
   ofd.AddExtension = true;
   ofd.Filter = "Drawing files|*.dwg";
   ofd.FilterIndex = 0;
   ofd.Multiselect = true;
   ofd.DefaultExt = ".dwg";
   if(ofd.ShowDialog() == DialogResult.OK && ofd.FileNames.Length > 0)
   {
      Application.DocumentManager.ForEach(ofd.FileNames,
         delegate(Document doc) 
         {
            doc.Editor.Command("._CIRCLE", new Point3d(2.0, 2.0, 0.0), 4.0);
            doc.Save();
         }
      );
   }
}

 

 

 

Message 3 of 6

Above, I speculated that the attached code (ContextManager.cs) should still work on all releases of AutoCAD, up to but excluding AutoCAD 2024 2025. I was mistaken. It works on all releases of AutoCAD, including AutoCAD 2025. Here's the amendment to the documentation that was added to the attached file:

 

 

 

/// 6-5-24:
/// 
/// ContextManager.cs was originally written to target AutoCAD
/// 2010 through 2018 (the current release when this code was
/// first published).
/// 
/// The code was originally published at:
/// 
///    https://forums.autodesk.com/t5/net/overkill-on-selection-set/m-p/7683139/highlight/true#M56841
/// 
/// Fast-forward 6+ years and low and behold, it still runs 
/// unmodified on AutoCAD 2025 under .NET 8.
/// 
/// Here are the steps taken to verify that the original
/// code still works on AutoCAD 2025:
/// 
/// 1.  Start Visual Studio and create a new project, specifying
///     Windows Forms Application as the new project type.
///    
/// 2.  Specify .NET 8.0 as the target framework.
/// 
/// 3.  Remove the Form1.cs file from the project.
/// 
/// 4.  Edit the project properties, and change the project's
///     Output Type from Windows Application to ClassLibrary.
///    
/// 5.  Remove Program.cs from the project.
/// 
/// 6.  Add references to these AutoCAD assemblies, and
///     set their CopyLocal property to false:
///    
///       AcCoreMgd
///       AcDbMgd
///       AcMgd
///       AcWindows
///       AdWindows
///      
/// 7.  Add ContextManager.cs (this file) to the project.
/// 
/// 8.  Build the project.
/// 
/// 9.  Start AutoCAD and NETLOAD the compiled assembly.
/// 
/// 10. Issue the DCPALETTE command.
///
/// Try using each of the examples behind the buttons on
/// the palette and note that all of them work flawlessly.

 

 

 

Message 4 of 6


@ActivistInvestor wrote:

Above, I speculated that the attached code (ContextManager.cs) should still work on all releases of AutoCAD, up to but excluding AutoCAD 2024. I was mistaken. It works on all releases of AutoCAD, including AutoCAD 2024.


You probably mean AutoCAD 2025, if you refer to .NET 8? 

Message 5 of 6

Yes, that was a mistake, I meant AutoCAD 2025/.NET 8.

Message 6 of 6

@ActivistInvestor Thank you very much for your solution. I also found that it works on AutoCAD 2024 as well. I'm implementing it and will circle back with specific questions. 

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

Post to forums  

Forma Design Contest


AutoCAD Beta