Excel VBA - Get selected points coordinates

Excel VBA - Get selected points coordinates

cool.stuff
Collaborator Collaborator
5,910 Views
11 Replies
Message 1 of 12

Excel VBA - Get selected points coordinates

cool.stuff
Collaborator
Collaborator

Hi!!!!

 

I'm trying to get the points coordinates from AutoCAD to Excel, using a Excel macro.

I have no experience in Excel API to acess AutoCAD data..

 

Could someone help me please? It would save me hours of work!! 🙂

 

Many many thanks!!! 🙂

 

0 Likes
Accepted solutions (2)
5,911 Views
11 Replies
Replies (11)
Message 2 of 12

grobnik
Collaborator
Collaborator
Accepted solution

 

You should modify the path for open a new template.

See picture below, you have to create a command button in excel, assign the macro below, and try.

 

Sub Macro_Cad()
    Dim acadApp As Object
'Check if AutoCAD is open.
   ' On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    On Error GoTo 0
    
    'If AutoCAD is not opened create a new instance and make it visible.
   If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
   End If
    
    'Check if there is an active drawing.
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    
    'No active drawing found. Create a new one.
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add("C:\Users\IO\AppData\Local\Autodesk\AutoCAD Mechanical 2021\R24.0\enu\Template\acad.dwt")
        acadApp.Visible = True
    End If
acadApp.Visible = True
MyScreenPoint = acadDoc.Utility.GetPoint(, "Select Insertion Point: ")
Range("E3").Value = MyScreenPoint(0)
Range("F3").Value = MyScreenPoint(1)
Range("G3").Value = MyScreenPoint(2)
End Sub

 

grobnik_0-1614961642927.png

 

Message 3 of 12

grobnik
Collaborator
Collaborator

If you want to avoid to insert path and become macro more generic, you can modify the macro as showed below.

strTemplatePath = ThisDrawing.Application.Preferences.Files.TemplateDwgPath
Set acadDoc = acadApp.Documents.Add(strTemplatePath & "\acad.dwt")

 

Message 4 of 12

cool.stuff
Collaborator
Collaborator

Many many thanks!! 🙂

 

Works like a charm! 🙂

 

Could you please make it work for the current point selection?

I have 30 or 40 points to extract each time I have to run distance verifications, it would be awesome! 🙂

 

It is possible please?

 

Many many thanks again! 🙂

0 Likes
Message 5 of 12

grobnik
Collaborator
Collaborator

Hi  thank you for your message.

I'll try, should be able to trap the selection event on autocad.

For above reason should be better transfer the macro on Autocad side instead inside Excel.

I'll try

Message 6 of 12

cool.stuff
Collaborator
Collaborator

Thank you for helping me!!!! 🙂

 

You'll try o do it in ACAD VBA?

I should be able to convert it to Excel VBA right? 🙂

0 Likes
Message 7 of 12

grobnik
Collaborator
Collaborator
No it's no possibile to convert, VBA base it's more or less the same, but
in order to trap the event you should be in Autocad development area.
However I'll try.
Message 8 of 12

grobnik
Collaborator
Collaborator
Accepted solution

Hi @cool.stuff 

here the code revised, I created a loop and Excel row increasing until you press "ESC" on Autocad for exit from point selection.

 

 

Sub Macro_Cad()
    Dim acadApp As Object
'Check if AutoCAD is open.
   ' On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
   ' On Error GoTo 0
    
    'If AutoCAD is not opened create a new instance and make it visible.
   If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
   End If
    
    'Check if there is an active drawing.
    'On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    
    'No active drawing found. Create a new one.
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add("C:\Users\IO\AppData\Local\Autodesk\AutoCAD Mechanical 2021\R24.0\enu\Template\acad.dwt")
        acadApp.Visible = True
    End If
acadApp.Visible = True
On Error Resume Next
A = 3
Here:
If Err <> 0 Then
        Err.Clear
        End
Else
    MyScreenPoint = acadDoc.Utility.GetPoint(, "Select Insertion Point: ")
    Range("E" & A).Value = MyScreenPoint(0)
    Range("F" & A).Value = MyScreenPoint(1)
    Range("G" & A).Value = MyScreenPoint(2)
    A = A + 1
End If
GoTo Here
End Sub

 

Let me know Bye

 

0 Likes
Message 9 of 12

cool.stuff
Collaborator
Collaborator

Your code works perfectly!!! 🙂

 

Many many thanks!!!

 

 

However, in my spreadsheet it does not work (it stops after the first points). This is due to some code I've written before... If I paste your code in a new spreadsheet it works perfectly!! 🙂

 

Maybe this is due to the Err.. Do you have any idea please?

 

Again, many many thanks!!! 🙂

0 Likes
Message 10 of 12

grobnik
Collaborator
Collaborator
Hi, could you pass me your spreadsheet?? Now I have some issue with
Autocad, but I'll try.
Did you assigned macro correctly? Share a screenshot of error got

Bye
0 Likes
Message 11 of 12

cool.stuff
Collaborator
Collaborator

Hi!!!! Many thanks for your answer!! 🙂

 

I would kindly give you the spreadsheet but it is from the company I work for..

I got no error 🙂

It just does not ask for more points. After the first iteration, it stops.

I dont know the remaining code contained in the spreadsheet..

As I said before, when I copy your macro to a new spreadsheet it works perfectly 🙂

0 Likes
Message 12 of 12

grobnik
Collaborator
Collaborator

Ok, 

You got no error due to there is an instruction "on error resume next" that allows you to skip the error but in the mean time you cannot have a debugging.

Try to use F8 function key and try to

run the procedure step by step pressing F8 on each instruction and try to check where is the error. 

With out any other element I cannot help you more.

0 Likes