.NET

.NET

Reply
Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 1 of 14 (1,710 Views)
Accepted Solution

How to select a drawing object which is in AutoCAD File from a .Net Application?

1710 Views, 13 Replies
07-17-2012 06:42 AM

Hi All,

 

I am trying to create a small application in C#.Net where by clicking a button, I should be able to select a drawing object in the AutoCAD file to get the object's handle.

 

I am able to open the Drawing file from the application but unable to figure out how to get the drawing object handle by clicking on it through the application.

 

Can anyone help with a code example ?

Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 2 of 14 (1,695 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-17-2012 08:34 AM in reply to: CodeBug

Okay Let's see If I understand: 

 

1- Click on AutoCAD Object (From Within AutoCAD)

2- Display the DBOject Handle or Get that String info and do something with it? 

 

  • One you'll need an Event  I believe it's DatabaseObjectSelected
  • Or PromptSelectionResult 

Okay so here's an example from the help file: slightly modified. 

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("SelectObjectsOnscreen")]
public static void SelectObjectsOnscreen()
{
  // Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // Start a transaction
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // Request for objects to be selected in the drawing area
      PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
 
      // If the prompt status is OK, objects were selected
      if (acSSPrompt.Status == PromptStatus.OK)
      {
          SelectionSet acSSet = acSSPrompt.Value;
 
          // Step through the objects in the selection set
          foreach (SelectedObject acSSObj in acSSet)
          {
              // Check to make sure a valid SelectedObject object was returned
              if (acSSObj != null)
              {
                  // Open the selected object for write
                  Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
                                                   OpenMode.ForWrite) as Entity;
if (acEnt != null) {
//This IS WHERE YOU CAN GET THE HANDLE
//Sorry I'm usually in VB.net so I don't know if this is right.
msgbox("My Entity's Handle is: " & acEnt.Handle);
// Change the object's color to Green acEnt.ColorIndex = 3; } } } // Save the new object to the database acTrans.Commit(); } // Dispose of the transaction } }

 Give it a try. If not we'll keep helping you to you get it working. 

 

Good Luck!

Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 3 of 14 (1,678 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-17-2012 11:51 PM in reply to: VB_Autocad_guy

Thanks VB_Autocad_guy! You got it almost right. I want the selected objet's hanlde.

 

But while I used your code, I am getting the  error below as

 

The specified module could not be found. (Exception from HRESULT: 0x8007007E)

 

in Main function at the line

 

Application.Run(new frmMain());

 

frmMain is the form name where the button is placed and your code in the button click event.

 

 

Any idea why this this error is shown ? So far what I have come to know is

"The error is occurring when the .Net runtime JITs method you're about to step into, because it couldn't find one of the types used by the method."

 

Any help is appreciated.

*Expert Elite*
norman.yuan
Posts: 986
Registered: ‎04-27-2009
Message 4 of 14 (1,664 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-18-2012 06:31 AM in reply to: CodeBug

You cannot run the code shown in the other reply in you standalone EXE application. The code can only run inside AutoCAD as NETLOADed .NET DLL.

 

If you have to do use EXE app, you would look into AutoCAD COM API (COM automation).

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 5 of 14 (1,664 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-18-2012 07:04 AM in reply to: norman.yuan
Imports Autodesk
Imports Autodesk.AutoCAD.Interop

Public Class SelectOnScreen

    Private Sub SelectOnScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SelectHandle()
    End Sub

    Sub SelectHandle()
        Dim acadDoc As AutoCAD.Interop.AcadDocument = Nothing

        Try
            acadApp = GetObject(, "AutoCAD.Application.18")
        Catch ex As Exception
        End Try

        If acadApp Is Nothing Then
            MessageBox.Show("Unable to Connect to AutoCAD", "AutoCAD Message", _
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim sset As AcadSelectionSet

        SsetMake(acadApp.ActiveDocument, "sset")
        sset = acadApp.ActiveDocument.SelectionSets.Item("sset")
        sset.SelectOnScreen()

        For Each item As Object In sset
            MsgBox(item.handle)
        Next

    End Sub

    Public Sub SsetMake(ByVal thisDrawing As AutoCAD.Interop.AcadDocument, ByVal ssetname As String)
        Dim sset As AcadSelectionSet
        Dim ssetcheck As Boolean
        For Each sset In thisDrawing.SelectionSets
            If sset.Name = ssetname Then
                ssetcheck = True
                Exit For
            End If
        Next
        If ssetcheck Then
            thisDrawing.SelectionSets.Item(ssetname).Delete()
            sset = thisDrawing.SelectionSets.Add(ssetname)
        Else
            sset = thisDrawing.SelectionSets.Add(ssetname)
        End If
    End Sub
End Class

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 6 of 14 (1,653 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-18-2012 12:22 PM in reply to: arcticad

Thanks Norman and articad for the help. 

 

Codebug - Hopefully you are up and running now!

Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 7 of 14 (1,633 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-18-2012 10:51 PM in reply to: VB_Autocad_guy

Thanks everyone!

 

VB_Autocad_guy:
I have converted the code by arcticad to C# and is definitly helpful but i'm waiting to see it working!

 

norman.yuan:

You pushed me back on track. thanks for that!

 

arcticad:
I have succesfully converted your code to C#.net but stuck at the line


For Each item As Object In sset
MsgBox(item.handle)     <<<<<<<<<<<<<<< Error line
Next


with error as

 

'object' does not contain a definition for 'handle'

 

I am not getting the Handle property for this object.

Please help..

 

Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 8 of 14 (1,615 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-19-2012 06:32 AM in reply to: arcticad

Also, SelectOnScreen without any arguments throws as eror as 

 

No overload for method 'SelectOnScreen' takes '0' arguments

 

What should be the arguments to this method ?

 

 

 


 


 

 

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 9 of 14 (1,607 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-19-2012 06:59 AM in reply to: CodeBug

just put null in the first argument.

 

Can you post your c# code?

Thanks

---------------------------



(defun botsbuildbots() (botsbuildbots))
Contributor
CodeBug
Posts: 18
Registered: ‎07-17-2012
Message 10 of 14 (1,593 Views)

Re: How to select a drawing object which is in AutoCAD File from a .Net Applicat

07-19-2012 09:48 PM in reply to: arcticad

Hi Arcticad,


This is my code.

 

internal void SelectHandle()
        {
           if (Globals.AcadDoc == null)
            {		        
                System.Windows.Forms.MessageBox.Show("Unable to Connect to AutoCAD");
		        return;
	        }

	        AcadSelectionSet sset = default(AcadSelectionSet);

            SsetMake(Globals.AcadDoc, "sset");
            sset = Globals.AcadDoc.SelectionSets.Item("sset");
            sset.SelectOnScreen();            
	        foreach (object item in sset) {                		        
                System.Windows.Forms.MessageBox.Show(item.handle);
               }
        }

 1. Error in line

 

sset.SelectOnScreen(); 

 

Error:

No overload for method 'SelectOnScreen' takes '0' arguments

 

2. Error in line

 

System.Windows.Forms.MessageBox.Show(item.handle);

 

Error:

'object' does not contain a definition for 'handle'

 

Thanks for staying with this!

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.