<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Best way to run custom scripts on multiple files in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9034384#M21306</link>
    <description>&lt;P&gt;Thanks &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1636004"&gt;@jabowabo&lt;/a&gt;! That's a bit like I have experimented with it as well. But won't all the files be opened at the same time when you do it like that since SendStringToExecute runs asynchronously?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code basically does:&lt;/P&gt;&lt;PRE&gt;            var script = "-purge\na\n*\nn\nqsave\nclose\n";

            var fileList = new List&amp;lt;string&amp;gt;()
            {
                "c:\\test\\1.dwg",
                "c:\\test\\2.dwg",
                "c:\\test\\3.dwg"
            };

            fileList.ForEach(file =&amp;gt;
            {
                var openDocument = DocumentCollectionExtension.Open(Application.DocumentManager, file, false);
                openDocument.SendStringToExecute(script, true, false, true);
            });&lt;/PRE&gt;&lt;P&gt;which for me is fast enough to open all the files before the scripts have finished...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to be able to run some code after the script finishes if it's possible to be able to control when to run the next file. Do you know any way of getting the script to run synchronously or to know when the script finishes somehow?&lt;/P&gt;</description>
    <pubDate>Thu, 19 Sep 2019 09:16:48 GMT</pubDate>
    <dc:creator>ottosson_mathias</dc:creator>
    <dc:date>2019-09-19T09:16:48Z</dc:date>
    <item>
      <title>Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9018500#M21299</link>
      <description>&lt;P&gt;I've seen applications like &lt;SPAN class="lia-message-unread"&gt;ScriptPro or&amp;nbsp;AutoScript and &lt;/SPAN&gt;I would like to try to write a similar application that can run scripts (as well as lisp scripts) on multiple files in AutoCAD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What's the best way to approach this problem?&lt;BR /&gt;&lt;BR /&gt;Let's say I write a purge script that I want to run on some files:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PURGE A * N
QSAVE&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;My files:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;myFile1.dwg
myFile2.dwg
myFile3.dwg&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should I write a .NET application that generates a "starter script" that opens all the files and runs the generated purge script?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;OPEN "c:/project/myFile1.dwg"
SCRIPT "c:/temp/genScript.scr"&lt;BR /&gt;CLOSE N
OPEN "c:/project/myFile2.dwg"
SCRIPT "c:/temp/genScript.scr"
CLOSE N&lt;BR /&gt;OPEN "c:/project/myFile3.dwg"
SCRIPT "c:/temp/genScript.scr"&lt;BR /&gt;CLOSE N&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and then invoke it by starting an AutoCAD process with the -b parameter:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;C:\Program Files\Autodesk\AutoCAD 2018\acad.exe -b C:\temp\starterScript.scr&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are there a more sophisticated way to solve this somehow?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know how this method holds up if you want to run a lisp script in the same fashion for example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it better to write a plugin that runs single lines with the SendStringToExecute command.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anybody knows how other applications have approached this problem?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2019 09:10:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9018500#M21299</guid>
      <dc:creator>ottosson_mathias</dc:creator>
      <dc:date>2019-09-11T09:10:59Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9018642#M21300</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;You should have a look at the AcCoreConsole.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://adndevblog.typepad.com/autocad/2012/04/getting-started-with-accoreconsole.html" target="_blank"&gt;https://adndevblog.typepad.com/autocad/2012/04/getting-started-with-accoreconsole.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-console.html" target="_blank"&gt;https://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-console.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.youtube.com/watch?v=ars7ZnCqpVA" target="_blank"&gt;https://www.youtube.com/watch?v=ars7ZnCqpVA&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2019 10:06:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9018642#M21300</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-09-11T10:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9018712#M21301</link>
      <description>&lt;P&gt;Accoreconsole is unfortunately very limited and won't work for my needs. It does not support any third party add-ons, or Express tools (which enabled diesel scripts). I need to be able to run the scripts in AutoCAD for this to work.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2019 10:44:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9018712#M21301</guid>
      <dc:creator>ottosson_mathias</dc:creator>
      <dc:date>2019-09-11T10:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9032535#M21302</link>
      <description>&lt;P&gt;Nobody who's got any suggestions? I've been struggling with this issue for a while now and I'm not closer to a solution yet...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would prefer to use an add-in that handles the script somehow.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried using the&amp;nbsp;`Document.SendStringToExecute` command, but since it is async it's awkward to work with, and I don't know any good way of using it even though it feels like it could be the solution somehow.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2019 13:33:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9032535#M21302</guid>
      <dc:creator>ottosson_mathias</dc:creator>
      <dc:date>2019-09-18T13:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9032806#M21303</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3097704"&gt;@ottosson_mathias&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Nobody who's got any suggestions?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just curious, why not use ScriptPro? Why reinvent the wheel?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2019 15:16:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9032806#M21303</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2019-09-18T15:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9032853#M21304</link>
      <description>&lt;P&gt;This is the way I do it - which is likely not the 'Best' way but I haven't discovered that one yet&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;      public static void RunMacro(string dwgFile, string macro)
        {
            macro = macro.Replace(Environment.NewLine, "\n");
            bool dwgIsOpen = false;

            DocumentCollection docs = AcadApp.DocumentManager;

            // Check if dwg is open
            foreach (Document d in docs)
            {
                if (d.Database.Filename == dwgFile)
                {
                    dwgIsOpen = true;

                    if (docs.MdiActiveDocument != d)
                        docs.MdiActiveDocument = d;

                    SendMacroToExecute(macro);
                    break;
                }
            }

            // If the dwg is not open then open it
            if (dwgIsOpen == false)
            {
                if (File.Exists(dwgFile))
                {
                    docs.Open(dwgFile, false);
                    SendMacroToExecute(macro);
                }
                else
                {
                    System.Windows.MessageBox.Show("File " + dwgFile + " does not exist.");
                }
            }
        }

        private static void SendMacroToExecute(string macro)
        {
            // send the macro text
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            doc.SendStringToExecute(macro, true, false, true);
        }&lt;/PRE&gt;
&lt;P&gt;And the calling code:&lt;/P&gt;
&lt;PRE&gt;string scrText = macroTextBox.Text;
List&amp;lt;string&amp;gt; dwgFiles = macroFilesListBox.Items.Cast&amp;lt;String&amp;gt;().ToList();

foreach (string dwgFile in dwgFiles)
{
  CommandTools.RunMacro(dwgFile, scrText);
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this usage case I have a text box with the macro commands and a list box with the dwg file names:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.PNG" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/678594iA111930B5F93E582/image-size/medium?v=v2&amp;amp;px=400" role="button" title="1.PNG" alt="1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2019 15:34:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9032853#M21304</guid>
      <dc:creator>jabowabo</dc:creator>
      <dc:date>2019-09-18T15:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9034332#M21305</link>
      <description>&lt;P&gt;I'm working on an application that does more than just scripting and that is very customized to our work flow. Scripting is an essential tool and I want to include it in the application to not have to install yet another one to just run scripts. I also would like to be able to fetch the command window output for each file to be able to process and present it in my application.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 08:19:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9034332#M21305</guid>
      <dc:creator>ottosson_mathias</dc:creator>
      <dc:date>2019-09-19T08:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9034384#M21306</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1636004"&gt;@jabowabo&lt;/a&gt;! That's a bit like I have experimented with it as well. But won't all the files be opened at the same time when you do it like that since SendStringToExecute runs asynchronously?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code basically does:&lt;/P&gt;&lt;PRE&gt;            var script = "-purge\na\n*\nn\nqsave\nclose\n";

            var fileList = new List&amp;lt;string&amp;gt;()
            {
                "c:\\test\\1.dwg",
                "c:\\test\\2.dwg",
                "c:\\test\\3.dwg"
            };

            fileList.ForEach(file =&amp;gt;
            {
                var openDocument = DocumentCollectionExtension.Open(Application.DocumentManager, file, false);
                openDocument.SendStringToExecute(script, true, false, true);
            });&lt;/PRE&gt;&lt;P&gt;which for me is fast enough to open all the files before the scripts have finished...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to be able to run some code after the script finishes if it's possible to be able to control when to run the next file. Do you know any way of getting the script to run synchronously or to know when the script finishes somehow?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 09:16:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9034384#M21306</guid>
      <dc:creator>ottosson_mathias</dc:creator>
      <dc:date>2019-09-19T09:16:48Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9034792#M21307</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3097704"&gt;@ottosson_mathias&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1636004"&gt;@jabowabo&lt;/a&gt;! That's a bit like I have experimented with it as well. But won't all the files be opened at the same time when you do it like that since SendStringToExecute runs asynchronously?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, and it's annoying. I've tried getting around it but haven't been successful.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 12:19:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9034792#M21307</guid>
      <dc:creator>jabowabo</dc:creator>
      <dc:date>2019-09-19T12:19:35Z</dc:date>
    </item>
    <item>
      <title>Re: Best way to run custom scripts on multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9049110#M21308</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1636004"&gt;@jabowabo&lt;/a&gt;yes, super annoying!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You'd think it would be a good idea to use the BeginDocumentClose event, but the document is still open when this event fires... And I haven't gotten the CloseWillStart event to fire when closing the documents, and I don't think that will work any better anyway, so... back at square one!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've seen other applications do this, so it shouldn't be impossible!&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 07:31:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/best-way-to-run-custom-scripts-on-multiple-files/m-p/9049110#M21308</guid>
      <dc:creator>ottosson_mathias</dc:creator>
      <dc:date>2019-09-26T07:31:15Z</dc:date>
    </item>
  </channel>
</rss>

