CommandMethod CommandFlags Conundrum

CommandMethod CommandFlags Conundrum

CommonGroundIndustries
Participant Participant
655 Views
1 Reply
Message 1 of 2

CommandMethod CommandFlags Conundrum

CommonGroundIndustries
Participant
Participant

My objective is to create a single command that opens a document, copies some info from inside of it, and then discards the document it opened. I want to do all of this from a single user command. Here's the gist of it:

 

<CommandMethod("MOCTB")>
Public Shared Sub runMOC()

     Dim nd = Application.DocumentManager.Add("C:\mydwg.dwg")
     Application.DocumentManager.MdiActiveDocument = nd
     Dim ed As Editor = nd.Editor

     ed.Command("COPYBASE", "0,0", "ALL", "")
     'run further commands, etc.
     '....

     'now finish up
      nd.CloseAndDiscard 'THIS line files with "drawing is busy"

End Sub

 

I understand that the line nd.CloseAndDiscard fails because the commandMethod needs the following parameter:

<CommandMethod("MOCTB", CommandFlags.Session)>

 

However with the CommandFlags.Session parameter set, the CloseAndDiscard works, but running ed.Command() now fails with "eInvalidInput"

 

<CommandMethod("MOCTB", CommandFlags.Session)> '<--needed for CloseAndDiscard to work
     Public Shared Sub runMOC()

     Dim nd = Application.DocumentManager.Add("C:\mydwg.dwg")
     Application.DocumentManager.MdiActiveDocument = nd
     Dim ed As Editor = nd.Editor

     ed.Command("COPYBASE", "0,0", "ALL", "") 'now this line fails with "eInvalidInput"
     'run further commands, etc.
     '....

     'now finish up
     nd.CloseAndDiscard

End Sub

 

How can I open a document, make changes in the document context, and then close it from the session context?

 

Any help is appreciated. 

 

 

0 Likes
656 Views
1 Reply
Reply (1)
Message 2 of 2

Norman_Yuan
Mentor
Mentor

Are you sure the code runs up to the line of 

nd.CloseAndDiscard()

?

 

The same code with my Acad2020/2021 stopped at ed.Command(...) with eInvalidImput exception. The reason is with the CommandMethod being without Session flag, even you have code to set the MdiActiveDocument to the newly opened document, is would not actually take effect until the current command ends. So, the Editor of newly opened document is not available during the command.

 

On the other hand, it is really easy to copy everything from an external Dwg file into another drawing, be it opens as document, or opens as side database. You can open the source dwg file as side database, collect all you want to copy, say, everything in ModelSpace, into an ObjectIdCollection and then call WBlockCloneObjects() to bring them into curretndocument's database, or a side database opened from another Dwg file. This way, you do not have open the source file in editor (thus, a lot faster), and no worries about using Session flag or not.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes