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

How to calculate distance between 2 points using COM Interop

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
ROBERTOYGLESIAS1067
5352 Views, 13 Replies

How to calculate distance between 2 points using COM Interop

Hi everyone,

 

I'm an advanced .Net developer but not very familiar with AutoCAD. I am developing an application that uses AutoCAD 2011 to check for some standards and I'm having trouble calculating the distance between 2 points using the .Net COM Interop. Here's the VBA line of code that I am migrating:

 

If Distance(tmpPt, LL_Pt) < 1# Then

13 REPLIES 13
Message 2 of 14

your points need to be gotten from the entities and cast into point3d objects. 

Autodesk.AutoCAD.Geometry.Point3D

then you can use point3DA.distanceTo(point3dB):

Distance = tempPoint.DistanceTo(TestPoint)

The entity has to be gotten from a transaction..., so how much do you know about ACAD .Net programming?

 

jvj

jvj
Message 3 of 14

Well but where would I import the Geometry namespace from? I don't see it as being part of the COM Interop? I've heard it's not a good idea (or good programming practice) to mix Interop and the API and that is an API DLL if I'm not mistaken.

Message 4 of 14

Your talking com interop... That's not going to be very wise.  Because ... Microsoft to Autodesk said no more 32bit api's.  So Autodesk to the rest of us said no more VBA (6.0).  That effectively kills Com interop, unless you want to wrap your own in a sweet little DLL.

 

Enough of that.  Let's back up.  Who is using this program?  Will it run inside or outside of AutoCAD (primarilly)?  If you are a savvy .Net developer, then I fully recommend getting into it via the .net dlls (acmgd.dll, acdbmdg.dll) found in the root folder of AutoCAD with the acad.exe file.  Much more support for that path, and a longer life time too.  Goto Autodesk website and look for developers section ("Partner with Autodesk") and look at AutoCAD product, then get your hands on ObjectARX developer's guide.  It has all the stuff for ObjectARX (lowest level C++) and its .Net wrapper (C#, and VB.net).

Also there is an online .Net guide for beginning with AutoCAD development.

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html

 

To sum up your code you must learn to:

<

CommandMethod("MyDocumentScruitinizer")>

public sub:

 Get the application (MDIActiveDocument, had to rename my namespace to AApplication)

 Get the document

doc As Document = AApplication.DocumentManager.MdiActiveDocument

 get the database

 Dim

 Dim db As Database = doc.Database

 start a transaction (Using trans as transaction = db.transactionmanager.starttransaction)

  get youre entity from the database:

  block table to

  block table record to

   list of object ids through

    entity = transaction.getobject(objectid,open for read)

     do your math here.

  trans.commit (IF YOU WANT TO CHANGE SOMETHING)

 END USING (trans)(unless you want to crash AutoCAD)

end sub.

 

good luck with all that!

 

jvj

 

check my posts out, I tend to give out fully functional code bits.  (also check out "JamieVJohnson")

jvj
Message 5 of 14

Thanks Jamie. Now, correct me if I'm wrong, but if what I'm doing (which is a .Net application that processes a batch of .dwg files to check for certain settings) will be used outside of AutoCAD, I have no choice but to either use the COM Interop or do a DLL using the API and then using the COM Interop do a Netload of that DLL and then execute a command in it. Am I completely missing the point here, can I actually use the .Net API to control AutoCAD from outside the application? (So far I've only done it as a compiled DLL that gets loaded through neload)

 

Really appreciate your help!

Message 6 of 14

There is support for using .net OUTSIDE of the running app.  (i've never done it.)  Look around in here, and you'll find it.  I believe the keyword is Real-DWG development ($$$).  The major problem is it takes AutoCAD's engine to interpret what the dlls are asking.  That hasn't stopped other developers, and if I needed to I would have learned all about it by now.

 

Correction, I have done the basics of what you ask.  If you have a computer that has AutoCAD Installed.  You can register your .net app (as an automatically running application when autoCAD starts) then run the commands you desire after you create, or hook into a running AutoCAD instance.

 

Or, After AutoCAD starts, have IT hook into a service you created outside of the program, then make majic happen.  Still you need to define commands within AutoCAD (your custom dll) to make things move.

 

jvj

jvj
Message 7 of 14

Jamie, I'm not sure I understand your correction, you mean that you still have to compile a DLL and put it inside AutoCAD, right? That is my problem since I need the results of several tests so that would mean writing to somewhere (probably a DB) and then getting those if I don't use the COM Interop. 

 

So I guess going back to my original question, if I wanted to calculate the distance between 2 points using COM Interop how would I do it? I've been able to do everything else besides that so far.

 

BTW, can I email or IM you (might be faster! 🙂 )

Message 8 of 14

still need the ACAD engine running.  or a dwg file reader.  the dll can be installed, automatically (writing to windows registry), As for the rest, see if there is any info in the VBA group (i worked in that group 4 years ago, but forgot much).  Going home...

 

jvj

jvj
Message 9 of 14

<quote>

Your talking com interop... That's not going to be very wise.  Because ... Microsoft to Autodesk said no more 32bit api's.  So Autodesk to the rest of us said no more VBA (6.0).  That effectively kills Com interop, unless you want to wrap your own in a sweet little DLL.

</quote>

 

It is incorrect. I think you are confusing Acad's COM API with VBA. VBA is not supported in 64-bit AutoCAD because of MS' decision on VBA, but AutoCAD's COM API is still there, well and alive.

 

Using COM API inside AutoCAD's managed DLL project is legitimate (well, it tends to be abused/overused when someone try to convert VBA app to managed DLL app), while for out-process exe app to automate AutoCAD, COM API is the only choice, which, it seems, is what the OP is doing.

 

To OP: calculating distance between 2 points is really a very basic math. While Point3d struct in AutoCAD managed API provided a ready to use method Point3d.DistanceTo(), you can easily calculate it (based on welknown formula c^2=a^2+b^2):

 

double dis=Math.Sqrt((X_poin2-X_poin1)*(X_poin2-X_poin1) + (Y_poin2-Y_poin1)*(Y_poin2-Y_poin1));

 

Message 10 of 14

Thanks Norman, that's exactly what I implemented and seems to work fine!

Message 11 of 14

Maybe I shed a little more light, or at least gain some insight into your software process.

 

Unless you are running RealDWG or a similar DWG file reader you will have a running session of AutoCAD, no matter how you cut it.  DLL, COM from external EXE, COM from internal DLL, whatever.  That *usually* means the best way of running automation is to create a .NET DLL that gets loaded into AutoCAD, which gives you quite a bit more access in the bargain.  You also don't have to worry about which session of AutoCAD is connected to (my users usually have a couple on the go at any given time), which version it is, whether it needs to be "hidden" from the user, and so on.

 

Now, you say you "need the results of several tests", across several drawings, yes?  Working across multiple drawings doesn't require you to do the work outside of AutoCAD (not sure if you realise this, just making sure).  In fact I think its easier to do this from inside AutoCAD in the first place.  No database, text file, or other external storage required.  The general process would be to loop over a list of file names (possibly opening them, possibly just loading the database) and pass each filename (or the active drawing if they are being opened) to a function which does your calculation.  The return value of the function can be stored in memory using an array, List (my favorite), or more complicated structure depending on how much information is returned.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 12 of 14

Thanks, now, is it possible to have the full power of .Net, meaning Windows Forms, WPF, access to Office and such from inside the AutoCAD DLL? Because that would be what I need.

 

Message 13 of 14
JamieVJohnson2
in reply to: dgorsman

Think less of Inside, and more like Beside.  AutoCAD will run any .net program you ask it too.  You can pop open windows that:

1. Float freely independant of AutoCAD's engine (form.open, window.open)

2. Float dialog windows, that make AutoCAD wait on you.  (read up on Document.Lock here)

3. Dock a window into an AutoCAD Palette (think tool palettes, properties palettes, and the layer palette). as a user control.

 

Your program can choose to use/not use AutoCAD within your program at your own whim.

 

jvj

jvj
Message 14 of 14

Yes, you can have all that, and more.  Working in .NET is a lot less linear than VBA or LSP, so it can be a bit of a conceptual "jump" to get your head around handling modeless forms, palettes (haven't even *started* looking into those yet), modular construction, iterating through documents, and the like.  Its one of the reasons I recommend keeping it all in the DLL - its one less thing that can go wrong.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


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