How to call AutoCAD Command with .net C# on runtime?

How to call AutoCAD Command with .net C# on runtime?

Anonymous
Not applicable
9,614 Views
9 Replies
Message 1 of 10

How to call AutoCAD Command with .net C# on runtime?

Anonymous
Not applicable

Dear All,

 

Season Greetings,

 

Needed experts help,

 

How to call AutoCAD Command with .net C# on runtime?

 

For Example,

 

After opening my Drawing File thro. .net C#,

 

                acApp.ActiveDocument.Application.ZoomExtents();

 

I need to use the AutoCAD command Oleopen

 

                acApp.ActiveDocument.BeginCommand.oleopen();

 

Here is some error.

 

Normally how to use the AutoCAD commands with .net C# programming.

 

Could you pleae help me?

 

regards,

Micky.

 

 

 

0 Likes
9,615 Views
9 Replies
Replies (9)
Message 2 of 10

Hallex
Advisor
Advisor

I think you can use SendCommand,

See this thread:

http://forum.dwg.ru/showthread.php?t=84180

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 3 of 10

Anonymous
Not applicable

Dear Mr. Hallex,

Thanks for your quick response,

In my coding,

 

<quote>  

conststring progID = "AutoCAD.Application.18";

           

AcadApplication acApp = null;

           

try

            {

acApp = (AcadApplication)Marshal.GetActiveObject(progID);

            }

           

catch

            {

               

try

                {

                   

Type acType = Type.GetTypeFromProgID(progID);

                    acApp = (AcadApplication)Activator.CreateInstance(acType,true);

                }

               

catch

                {

                   

MessageBox.Show("Cannot create object of type \"" + progID + "\"");

                }

            }

           

if (acApp != null)

            {

                acApp.Visible =true;

                acApp.ZoomExtents();

                acApp.ActiveDocument.SendCommand("oleopen\r");

            }

 

<unquote>

 

the error which i am getting is

 

<quote>

 

Command: oleopen


Unable to find OLE object.  Object must be selected before entering the
OLESCALE command.

 

<unquote>

 

I have tried with the reference link sent by you,

 

Similarly I have tried to select the Ole Object and then to SendCommand "Oleopen", But I am getting some errors.

 

<quote>

 

 using (Transaction t = db.TransactionManager.StartTransaction()) {
BlockTableRecord bt = t.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId id in bt) {
if (id.ObjectClass.Name != "AcDbOle2Frame")
continue;
Ole2Frame oleFrame = t.GetObject(id, OpenMode.ForRead) as Ole2Frame;
IntPtr ptrClientItem = AcDbOle2Frame_getOleClientItem(oleFrame.UnmanagedObject);
COleClientItem_CopyToClipboard(ptrClientItem, 0);
}
t.Commit();

 

<unquote> 

 

Could you please help me?

 

regards,

Micky.

Message 4 of 10

Hallex
Advisor
Advisor

If this ole object was inserted to model spce

try this code, tested on my A2010

Don't forget to release AcadApplication at the end of code

 

      public void TestFindOle()
       {
       const string progID = "AutoCAD.Application.18";

           

AcadApplication acApp = null;

           

try

            {

acApp = (AcadApplication)Marshal.GetActiveObject(progID);

            }

           

catch

            {

               

try

                {

                   

Type acType = Type.GetTypeFromProgID(progID);

                    acApp = (AcadApplication)Activator.CreateInstance(acType,true);

                }

               

catch

                {

                   

MessageBox.Show("Cannot create object of type \"" + progID + "\"");

                }

            }

           

if (acApp != null)

            {

                acApp.Visible =true;

                
    AcadDocument AcadDoc = acApp.ActiveDocument;
    
    acApp.Documents.Open(@"C:\Test\mtext.dwg", Type.Missing, Type.Missing);
        AcadModelSpace AcSpace = AcadDoc.ModelSpace;
        acApp.ZoomExtents();
        AcadOle AcOle = null;
            //loop through the model space to fill the list of block references
            foreach (AcadEntity AcEnt in AcSpace)
            {
                if (AcEnt is AcadOle)
                {
             
                    AcOle  = AcEnt as AcadOle;
                  if (AcOle != null)
                  {
                      string hand = AcOle.Handle;
                      acApp.ActiveDocument.SendCommand("select " + "(handent " + "\"" + hand + "\"" + ")  ");
                     
                      acApp.ActiveDocument.SendCommand(string.Format("_oleopen  \"{0}\" \r","L"));
                  }
                    
                }
            }


            }

       }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 5 of 10

Anonymous
Not applicable

Dear Sir,

 

Thanks very much for your support,

 

Since my OLE object is in Paper space,

 

I just modified your coding to paper space only,

 

everything working well but ole open command is not working,

 

please see the below coding,

 

<quote>

 

if

(acApp != null)

            {

                acApp.Visible =

true;

               

AcadDocument doc1 = acApp.ActiveDocument;

                acApp.Documents.Open(

"D:/test.dwg");

                acApp.Application.Visible =

true;

                acApp.Application.WindowState = Autodesk.AutoCAD.Interop.Common.

AcWindowState.acMax;

                acApp.WindowState = Autodesk.AutoCAD.Interop.Common.

AcWindowState.acMax;

                acApp.ActiveDocument.Application.ZoomExtents();      

             

               Autodesk.AutoCAD.Interop.Common.

AcadPaperSpace AcSpace = doc1.PaperSpace;

               Autodesk.AutoCAD.Interop.Common.

AcadOle AcOle = null;

               

//loop through the model space to fill the list of block references

               

foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity AcEnt in AcSpace)

                {

                   

if (AcEnt is Autodesk.AutoCAD.Interop.Common.AcadOle)

                    {

                        AcOle = AcEnt

as Autodesk.AutoCAD.Interop.Common.AcadOle;

                       

if (AcOle != null)

                        {

                           

string hand = AcOle.Handle;

                            acApp.ActiveDocument.SendCommand(

"select " + "(handent " + "\"" + hand + "\"" + ")  ");

                            acApp.ActiveDocument.SendCommand(

string.Format("_oleopen  \"{0}\" \r", "L"));

                        }

                    }

                }

 

 

            }

           

MessageBox.Show("tested OK");

 

<unquote>

 

best regards,

Micky.

0 Likes
Message 6 of 10

Hallex
Advisor
Advisor

What is the ole object you've inserted in your drawing?

I'm using Excel for my test

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 7 of 10

Anonymous
Not applicable

Dear Sir,

 

I also using Excel as a OLE object.

 

programming is working well without any errors.

 

but only the ole open is not popping up.

 

regards,

Micky

0 Likes
Message 8 of 10

Hallex
Advisor
Advisor

Hm, it's strange, can you upload the sample Excel file and drawing with Ole object,

btw I'm using A2010 and Excel 2007 (student release)

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 9 of 10

Anonymous
Not applicable

Dear Sir,

 

the file size is around 2 MB, while uploading there is an error.

 

could you please send me your Email Id so that let me send it to your Email.

 

best regards,

Micky.

 

 

 

0 Likes
Message 10 of 10

Anonymous
Not applicable

Dear Sir,

 

Season Greetings,

 

Sorry to bother you.

 

since the drawing file size is around 2 MB, there is a error while uploading.

 

Could you please send me your Email Id to send the drawing file?

 

I think the problem is in selecting the OLE object,

 

the program was ignoring the selection,

 

<quote>

 

               Autodesk.AutoCAD.Interop.Common.AcadPaperSpace AcSpace = doc1.PaperSpace;

               Autodesk.AutoCAD.Interop.Common.AcadOle AcOle = null;

                //loop through the model space to fill the list of block references                 foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity AcEnt in AcSpace)                 {                     MessageBox.Show("Foreach OK");

                    if (AcEnt is Autodesk.AutoCAD.Interop.Common.AcadOle)                     {                         AcOle = AcEnt as Autodesk.AutoCAD.Interop.Common.AcadOle;                         if (AcOle != null)                         {                             string hand = AcOle.Handle;                             acApp.ActiveDocument.SendCommand("select " + "(handent " + "\"" + hand + "\"" + ")  ");

                            MessageBox.Show("OLE selected..");

                            acApp.ActiveDocument.SendCommand(string.Format("_oleopen  \"{0}\" \r", "L"));                         }                     }                 }

 

<unquote>

 

best regards,

Micky.

0 Likes