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

Synchronous job

8 REPLIES 8
Reply
Message 1 of 9
thierry_prince
698 Views, 8 Replies

Synchronous job

Hi,

 

A customer try to do an action on the current drawing and then to open the new drawing created as result.

Infortunately, AutoCAD crashes when opening the new drawing.

 

For its tests, he uses AutoCAD Map 3D 2013 and a C# application writen with VS 2010.

 

He find some code on the web to do his job :

// We create the map in a new drawing
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
doc.Editor.Command("-MAPTOACAD", "M", "AutoCAD-2010-Drawing", "C:\export.dwg", "N", " ");

// We open the result
System.Diagnostics.Process.Start("C:\export.dwg")

 

All our applications are written in C++ and we have no experience in C#, so it is difficult to help him.

 

Thanks in advance for your answers.

 

Thierry

8 REPLIES 8
Message 2 of 9
scartlamy
in reply to: thierry_prince

Hi,

I'm the customer.

 

The purpose of this is to export current Map (with FDO layers) to classic DWG and then open it.

 

I can't use Document.SendStringToExecute() function because it's asynchronous and we can't find when export is finished (even with the events CommandEnd).

 

Otherwise, as Thierry said, the Document.Editor.Command() makes AutoCAD to crash.

 

Do you have any idea ?

 

Thanks

 

S.Cart-Lamy

Message 3 of 9
AubelecBE
in reply to: scartlamy

i have the same issue with etransmit.

so i have add the 2° command in the command line of etransmit.

 

my code :

 

acApp.SetSystemVariable("FILEDIA", 0)
                            doc.SendStringToExecute("-etransmit" & vbCr & _
                                                    "CH" & vbCr & _
                                                    "archivage" & vbCr & _
                                                    "C" & vbCr & _
                                                     "" & Txt & vbCr & _
                                                     "FILEDIA" & vbCr & "1" & vbCr, True, False, True)

 

you could send your command (here replace -etransmit) and after you command for opening the file.)

all this execute after terminanting your code.

 

i have see this too :

http://through-the-interface.typepad.com/through_the_interface/2014/03/autocad-2015-calling-commands...

 

Message 4 of 9
scartlamy
in reply to: AubelecBE

Thanks for this answer.

 

With your solution, the command do the export and then the DWG is open correctly (obviously).

 

But it is not synchronous command. Indeed, I do things before and after the command, like this :

 

// Do some stuff here (disable some layers, create temp Map, duplicate layers, change name...)

// Export

Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("FILEDIA", 0);
doc.SendStringToExecute("-MAPTOACAD M AutoCAD-2010-Drawing " + outputFile + " N  ", true, false, true);
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("FILEDIA", 1);

// Do post export stuff, restoring original layers state (delete temp Map, enable layers...)

 

 

Thus, my post export code must be executed AFTER the end of the command... and unfortunately FILEDIA does not block code exécution.

 

Anyway, thanks for the tip.

 

Message 5 of 9
scartlamy
in reply to: scartlamy

I saw the article of Kean Walmsley, but nothing is suitable for AutoCAD 2013... He wrote about doc.Editor.Command but as I said, it makes AutoCAD to crash

Message 6 of 9
AubelecBE
in reply to: scartlamy

hi in my exmple the double commande is :

- etransmit

- filedia set to 0.

 

i need to set fildia to 0 for pass information for the command etransmit.

 

but etransmit is assyncrone so the commande filedia to 1 not working if i use the command in the vb.net to do that i have to integrate de command in the string of the command of the etransmit.

 

but in your case do you can :

1 - Do some stuff here

2 - save the file in the temporary folder.

3 - export

 

4 - close the file without saving. (not needing it is a tempory file)

 

Message 7 of 9
scartlamy
in reply to: AubelecBE

Hi,

I think your idea may work.

 

I tried this :

  • Do my stuff into my doc
  • Save my doc into a temp DWG
  • Open it with DocumentManager
  • Run my export command on this temp doc (-MAPTOACAD...) to another final file
  • Open final file

It work until export included : ACAD create a good file with all layers.

 

Unfortunately, after the export command, the thread seems to be in sleep mode, and the command which open the final file (System.Diagnostics.Process.Start) is never executed, neither all stuf I do after that.

 

I don't really understand how ACAD works with the thread which executes user code... Moreover, you can't create other threads because you cannot interact with GUI and ACAD commands.

 

Do you understand how it works ?

 

Thanks

Message 8 of 9

AutoCAD2013 (or AutoCAD Map 2013) does not support Editor.Command()/CommandAsyc(): they only available since AutoCAD 2015 (Yes, Editor.CommandAsyc() would be of help in your situation).

 

However, as I can see, your command string you used in Editor.Command(), which would be the same when you tried with SendStringToExecute(), I assume, is wrong: the second argment should be "E" (for Editable) or "V" (for Visuals) while you user "M". That could be reason that your SendStringToExecute() does nothing, thus no new drawing is generated.

 

I wrote a piece of simple code and tested. Yes, you can use SendStringToExecute() and handle CommandEnd event to achieve your goal of exporting map objects into a native drawing and open the drawing after the exporting. Here is the code:

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(OpenFileAfterSendStringToExecute.MyCadCommands))]

namespace OpenFileAfterSendStringToExecute
{
    public class MyCadCommands
    {
        private static string outDwg = "";
        private static bool cmdDone = false;

        [CommandMethod("M2A")]
        public static void RunMyCadCommand()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            outDwg = 
                    @"C:\Temp\TestDwg.dwg";

            if (System.IO.File.Exists(outDwg))
            {
                System.IO.File.Delete(outDwg);
            }

            try
            {
                cmdDone = false;
                dwg.CommandEnded += dwg_CommandEnded;
                Application.Idle += Application_Idle;

                string cmd = "-MAPTOACAD E AutoCAD-2013-Drawing " + 
                    outDwg + " N " + string.Empty + " N ";
                dwg.SendStringToExecute(cmd, true, false, true);
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}\n{1}", ex.Message, ex.StackTrace);
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        static void Application_Idle(object sender, EventArgs e)
        {
            if (cmdDone)
            {
                if (System.IO.File.Exists(outDwg))
                {
                    Application.DocumentManager.Open(outDwg, false);
                    cmdDone = false;
                }
            }

            Application.Idle -= Application_Idle;
        }

        static void dwg_CommandEnded(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName.ToUpper().Contains("MAPTOACAD"))
            {
                Document dwg = CadApp.DocumentManager.MdiActiveDocument;
                Editor ed = dwg.Editor;
                ed.WriteMessage("\nMapToAcad command ended!");

                cmdDone = true;

                dwg.CommandEnded -= dwg_CommandEnded;
            }
        }
    }
}

 To run the code, you need to open a drawing with layer(s) of map objects.

 

HTH.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 9 of 9
scartlamy
in reply to: norman.yuan

Hi,

After a month of testing your solution, it works !

 

Thanks for this !

 

Regards

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost