.NET

.NET

Reply
Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 1 of 10 (2,463 Views)

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

2463 Views, 9 Replies
09-03-2012 12:20 AM

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.

 

 

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 10 (2,440 Views)

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

09-03-2012 01:01 PM in reply to: Micky_1

I think you can use SendCommand,

See this thread:

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

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 3 of 10 (2,421 Views)

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

09-04-2012 02:52 AM in reply to: Hallex

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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 10 (2,417 Views)

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

09-04-2012 03:41 AM in reply to: Micky_1

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
Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 5 of 10 (2,414 Views)

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

09-04-2012 04:31 AM in reply to: Hallex

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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 10 (2,411 Views)

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

09-04-2012 05:01 AM in reply to: Micky_1

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

I'm using Excel for my test

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 7 of 10 (2,405 Views)

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

09-04-2012 06:13 AM in reply to: Micky_1

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

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 8 of 10 (2,392 Views)

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

09-04-2012 07:19 AM in reply to: Micky_1

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
Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 9 of 10 (2,385 Views)

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

09-04-2012 09:12 AM in reply to: Hallex

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.

 

 

 

Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 10 of 10 (2,339 Views)

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

09-05-2012 01:42 AM in reply to: Hallex

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.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.