.NET

Reply
Mentor
bsee1
Posts: 212
Registered: ‎11-14-2011
Message 1 of 4 (350 Views)
Accepted Solution

SendCommand in AutoCAD2010 send to the wrong drawing

350 Views, 3 Replies
01-17-2014 12:55 PM

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

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.

 

*Expert Elite*
norman.yuan
Posts: 1,049
Registered: ‎04-27-2009
Message 2 of 4 (334 Views)

Re: SendCommand in AutoCAD2010 send to the wrong drawing

01-17-2014 03:25 PM 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.

 

Distinguished Mentor
gasty1001
Posts: 581
Registered: ‎04-11-2010
Message 3 of 4 (331 Views)

Re: SendCommand in AutoCAD2010 send to the wrong drawing

01-17-2014 03:29 PM in reply to: bsee1

Hi,

 

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

 

Gaston Nunez

 

Mentor
bsee1
Posts: 212
Registered: ‎11-14-2011
Message 4 of 4 (274 Views)

Re: SendCommand in AutoCAD2010 send to the wrong drawing

01-20-2014 06:14 AM 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!