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

SendCommand in AutoCAD2010 send to the wrong drawing

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
bsee1
760 Views, 3 Replies

SendCommand in AutoCAD2010 send to the wrong drawing

Overall goal is to set the dimscale of the drawing to 4.0. I wasn't able to make this happen in C#, so I'm now taking the approach of SendCommand or SendStringToExecute.

 

This is a batch command.  I have a default drawing called drawing1.dwg, then I have my programmatically opened drawing using this code: Document openDoc = acDocMgr.Open(fileName, false);

 

When I use openDoc.SendStringToExecute("DIMSCALE " + dimScale + " ",true,true,true); it sends to to the default drawing...even though I specifically sent it to openDoc. Why?  This also happens if I use the following:

 

acDocMgr.MdiActiveDocument = openDoc;

acadDoc.SendCommand("DIMSCALE " + dimScale + " ");

 

Can someone help?  I'm either looking for a programmatic way to set the drawing dimscale, or a way for sendcommand to go to the correct drawing.

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
3 REPLIES 3
Message 2 of 4
norman.yuan
in reply to: bsee1

How is your CommandMethod started to run your code? Do you have CommandFlags.Session flag set?

 

If your command method does not have Session flag set, the opened drawing will not become MdiActiveDocument. SendStringToExecute()/SendCommand() always executes the command on current document (MdiActiveDocument). It is also the cose if you use Application.SetSystemVariable().

 

If you do not need to make the opened drawing becomes MdiActiveDocument, you can simply set Database.Dimscale property.

 

If you want the opened document to be MdiActiveDocument (for example, after setting DicScale, you want to do something else to the opened drawing), you need to set Session flag to the CommandMethod.

 

See following 2 code samples:

 

[CommandMethod("MyCmd1")]
        public static void RunMyCommand1()
        {
            string[] dwgFiles = new string[]
            {
                @"C:\Temp\DrawingTest1.dwg",
                @"C:\Temp\Drawingtest2.dwg"
            };

            DocumentCollection docs = Application.DocumentManager;

            int i = 0;
            foreach (var f in dwgFiles)
            {
                Document dwg = docs.Open(f, false);
                Application.ShowAlertDialog("Current DIMSCALE: " + dwg.Database.Dimscale.ToString());
                using (DocumentLock l = dwg.LockDocument())
                {
                    double scale = i == 0 ? 4.0 : 5.0;
                    dwg.Database.Dimscale = scale;
                }
                Application.ShowAlertDialog("Changed DIMSCALE: " + dwg.Database.Dimscale.ToString());

                i++;
            }
        }

 

[CommandMethod("MyCmd2", CommandFlags.Session)]
        public static void RunMyCommand2()
        {
            string[] dwgFiles = new string[]
            {
                @"C:\Temp\DrawingTest1.dwg",
                @"C:\Temp\Drawingtest2.dwg"
            };

            DocumentCollection docs = Application.DocumentManager;
            int i = 0;
            foreach (var f in dwgFiles)
            {
                Document dwg = docs.Open(f, false);
                docs.MdiActiveDocument = dwg;
                Application.ShowAlertDialog("Current DIMSCALE: " + dwg.Database.Dimscale.ToString());
                double scale = i == 0 ? 4.0 : 5.0;
                using (DocumentLock l = dwg.LockDocument())
                {
                    Application.SetSystemVariable("DIMSCALE", scale);
                }
                Application.ShowAlertDialog("Current DIMSCALE: " + dwg.Database.Dimscale.ToString());
                i++;
            }
        }

 

I deliberately made the 2 opened drawings have different DIMSACLE, so that after the command I can verify the DimScale is set correctly.

 

There is no need to call SendStringToExecute(), and especially no need to go to COM's SendCommand(). However, do not for get lock the document before making change to the document.

 

HTH.

 

Message 3 of 4
hgasty1001
in reply to: bsee1

Hi,

 

Did you try setting "DIMSCALE" this way: Application.SetSystemVariable("DIMSCALE", ScaleValueHere) ?

 

Gaston Nunez

 

Message 4 of 4
bsee1
in reply to: norman.yuan

I didn't realize I needed the session flag to make the activedocument command work.  Also, setting Dimscale through the database is much better than using sendcommand.  The Inventor api is much more intuitive than AutoCAD. Simple things, like just trying to find an attribute and its tag on a drawing are far more complicated that I would think they should be.

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1

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