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

Selecting entities in AutoCAD 2009 from C# Visual Studio 2008

25 REPLIES 25
Reply
Message 1 of 26
Elie911
887 Views, 25 Replies

Selecting entities in AutoCAD 2009 from C# Visual Studio 2008

Hi,

in my graduation project, I need to prompt the user from a C# Windows Application to select a line so I can access its properties .
It seems that no one in my university knows a solution for this problem .
Can I find it here ??

Plz , I really need your help .

Using AutoCAD 2009 and Visual Studio 2008 .

Best Regards ...
25 REPLIES 25
Message 2 of 26
chiefbraincloud
in reply to: Elie911

Most if not all of the entity selection methods are under the Editor object in the EditorInput namespace.

I didn't try, but I'm sure if you search this forum for entity selection, you'll find plenty of examples.
Dave O.                                                                  Sig-Logos32.png
Message 3 of 26
Elie911
in reply to: Elie911


Thank you for your reply , but when I put the namespace EditorInput , the following error appear :



The type or namespace name 'EditorInput' does not exist in the namespace 'AutoDesk.AutoCAD' (are you missing an assembly reference?)



N.B. I added all the references concerning AutoCAD 2009 .



What must I do ???

Message 4 of 26
Anonymous
in reply to: Elie911


You cannot use EditorInput namespace, which is in
ObjectARX .NET APIs, from external exe application, that automates
AutoCAD via COM API.

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">


Thank you for your reply , but when I put the namespace EditorInput ,
the following error appear :



The type or namespace name
'EditorInput' does not exist in the namespace 'AutoDesk.AutoCAD' (are you
missing an assembly reference?)



N.B. I added all the
references concerning AutoCAD 2009 .



What must I do ???

Message 5 of 26
chiefbraincloud
in reply to: Elie911

The EditorInput Namespace is located in acmgd.dll. If you have that referenced, maybe Norman is right about what you are trying to do?
Dave O.                                                                  Sig-Logos32.png
Message 6 of 26
Elie911
in reply to: Elie911

Do you mean that I have to change my hole project ???

There must be a better solution ...
Message 7 of 26
Elie911
in reply to: Elie911

I just want to know where I can find the acmgd.dll so i can reference it . It does not appear in the references window and I don't think I hav referenced it yet .

Plz guide me as soon as possible .



Best Regards ...
Message 8 of 26
Elie911
in reply to: Elie911

I found the acmdg.dll and I added it to the references list .

Many commands are added as expected but if I find a difficulty to write the code .

I'll be very thankfull if someone can send me an example for selecting a line on the screen and obtaining its length .



I'm using C# Visual Studio 2008 .



Best Regards ...
Message 9 of 26
norman.yuan
in reply to: Elie911

Have you read my reply?

You CANNOT use EditorInputs namespace or set reference to acmgd.dll in your Windows exe application. Many topics discussed in this NG sipmly do not appy to you. This NG is meant for ObjectARX .NET APIs, while your exe app can only talk to AutoCAD via COM APIs, in spite you use C#. So, unless you change your whole design (automating AutoCAD through an external exe app), you can only look into COM object model's AcadSelectionSet.Select() method, as you do in VBA, well, if you have VBA experience

As matter of fact, running an exe application, and ask user switch to AutoCAD to select something in AutoCAD screen, is really not a favoured approach, to say the least, I'd say it is rather bad approach in most cases.

Norman Yuan

Drive CAD With Code

EESignature

Message 10 of 26
Elie911
in reply to: Elie911

Thank you Norman for your replies.



I started my project using VBA but I than chose the C# as it's more developped in forms and its toolbox is more advanced .



It took me 2 months to recover the informations about C# so I can master it as I am a mechanical engineer .



I have only 1 month left for my project and I have to start all over again, so if you can give me the code of the selecting of a line and having its length in VBA, I will be very thankfull , it will save me a lot of time.



I really need your help ...



Thanks in advance ...
Message 11 of 26
norman.yuan
in reply to: Elie911

Here is some VBA code (not tested, though), here I used GetEntity() instead of AcadSelectionSet for single entity selewction. If it is possible to select more than one entity, you may look into SelectionSet with proper filters defined

{code}

Public Sub ChangeLine()

Dim ent As AcadEntity
Dim pt As Variant
Dim line As AcadLine
Dim eP(0 to 2) As Double

ThisDrawing.Utility.GetEntity ent, pt, "vbCr & "Please pick a line: "

''You need to write sone validation code to handle situation like when user hits "ESC" to cancel the pick, or picked blank space...
''Thus to make sure an AcadEntity is really picked.

''Then, make sure the picked entity is AcadLine (or whatever you expected), not something else
If TypeOf ent Is AcadLine Then
Set line=ent
''Do what ever you need to, such as change its end point:
eP(0)=20#:eP(1)=30#:eP(2)=0#
line.EndPoint=eP
line.Update
Else
MsgBox "Picked entity is not an AcadLine!"
End If

End Sub

{code}

You should be able translate to C# code for your exe app.

Again, it is bad idea to ask user switch from your exe app to AutoCAD to pick something. You make notice when your code run into the point when use is to pick, your exe app at this moment is the active window app, user has to first make the running AutoCAD active window app, then has to pick the Acad editor again to activate Acad's document window, then user can do the pick. After this, user may have to make your exe app active again in order to go back. This whole thing gives very awful user experience. Trying to use code to active different windows app (yours and AutoCAD) at needed moments may improve the user experience, but it is very difficult to do, considering your programming experience, or even not doable.

Norman Yuan

Drive CAD With Code

EESignature

Message 12 of 26
norman.yuan
in reply to: Elie911

Sorry,to a typo:

ThisDrawing.Utility.GetEntity ent, pt, "vbCr & "Please pick a line: "

Should be

ThisDrawing.Utility.GetEntity ent, pt, vbCr & "Please pick a line: "

Norman Yuan

Drive CAD With Code

EESignature

Message 13 of 26
Elie911
in reply to: Elie911

Thank you very much Norman .

I'll try them and I'll report as soon as possible ...



I need some time as u know to translate the whole code ...
Message 14 of 26
chiefbraincloud
in reply to: Elie911

go here:

http://www.developerfusion.com/tools/convert/vb-to-csharp/

To clarify, are you building a .dll or an .exe?
Dave O.                                                                  Sig-Logos32.png
Message 15 of 26
Elie911
in reply to: Elie911

Thank you for the link but I have a similar one working offline, what takes time is to redesign the userforms : textboxes, labels.....

and I'm having a problem with vba toolbox where there is not a datagrid like Visual Studio so I used a list box and I'm not able yet to read from Excel, can you give me any suggestions ??

For what I am building, I need the user to enter in the command line a keyword as it goes for " vbaide" and the userform I designed to appear so the user select some entities on the AutoCAD modelspace and enter some input on the form so i can deal with them ...

Best Regards ...
Message 16 of 26
norman.yuan
in reply to: Elie911

There are usually 2 ways to access data in a Excel spreadsheet file:

1. COM automating Excel application. You can find lots of example in Acad VBA NG or from the net. In this case, Excel must be installed. IMO, if the accessing is only for reading/writing data (i.e. you do not have to manipulating cell's format/look), this is not good option.

2. Using DAO/ADO directly read data from *.xls file without opening it in Excel application. In this case, Excel installation is not required. You can also find lots of example VB code from the net.

So, you need to understand, do you want to get data from *.xls file, or do you need to communicate with Excel application.

Not knowing what exactly your project is to do, it is difficult to have meaningful comment.

Norman Yuan

Drive CAD With Code

EESignature

Message 17 of 26
Elie911
in reply to: Elie911

My project goal is to acquire an AutoCAD map where I have to prompt the user to select every wall of every room to choose his composant materials so I can find the value for the energy loss, it's an HVAC project where I use many Excel workbook but just to read from , the manipulation will start in VBA code .



I have already Excel 07 installed if it's easier for you to guide me .





Thank you ...
Message 18 of 26
norman.yuan
in reply to: Elie911

Googling "Read Excel With ADO" would bring you a lot of links. Here is one you can use directly:

http://support.microsoft.com/kb/257819

If you do want to go the Excel automating route, googling for "Automating Excel", here is one example:

http://www.eggheadcafe.com/tutorials/aspnet/8c6de397-f6d1-4696-919b-e50311dbd917/automating-excel.aspx

Norman Yuan

Drive CAD With Code

EESignature

Message 19 of 26
Anonymous
in reply to: Elie911

Elie911 schreef:
> My project goal is to acquire an AutoCAD map where I have to prompt the
> user to select every wall of every room to choose his composant
> materials so I can find the value for the energy loss, it's an HVAC
> project where I use many Excel workbook but just to read from , the
> manipulation will start in VBA code .
>
>
>
> I have already Excel 07 installed if it's easier for you to guide me .
>
>
>
>
>
> Thank you ...

I would suggest using the following setup:

- Office Open XML for talking with Excel 2007 (beats both the VBA and
COM approach):
http://www.15seconds.com/issue/070111.htm
http://msdn.microsoft.com/en-us/library/bb332058.aspx

- .NET API for AutoCAD

Then do everything from within AutoCAD. So you don't have to bother with
the COM API...

Of course, if you already started out your project, you might want to
keep the existing code, and this won't be very helpful 🙂

If you have lots of forms, already created in VB, and you want to
continue in C# without having to redo the forms, I'd suggest using an
MVC or MVP approach (just look 'em up). This way you can keep the forms
with minor adjustments (in a seperate dll).

And you'll score points just for using the MVP or MVC pattern as well 🙂


--
http://cupocadnet.blogspot.com
Message 20 of 26
Elie911
in reply to: Elie911

hello , here is the code I used for filling a multicolumn listbox from an excel workbook :




Private Sub UserForm_initialize()





Dim app As excel.Application

Dim wbook As excel.Workbook

Dim wsheet As excel.Worksheet

Dim rg As excel.Range



Set app = excel.Application

Set wbook = app.Workbooks.Open("C:\Materials.xlsx")

Set wsheet = wbook.Worksheets(1)

Set rg = wsheet.Range(A1, D107)



Dim iCol As Integer

iCol = 2

Dim iRow As Integer



For iRow = 1 To 10 Step iRow + 1

ListBox1 = wsheet.Cells(iRow, 1).Value

Next

End Sub



but an error is appearing :



Run-time error '1004':

Method 'Range' of object '_worksheet' failed



plz help !!!

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