VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I select the current object in VBA code, in Autocad?

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
Markintx
9421 Views, 11 Replies

How do I select the current object in VBA code, in Autocad?

I am trying to cycle thru and select each item and then perform actions on them.

What I have right now is..

 

  For Each ObjEnt In ThisDrawing.ModelSpace
 
   If TypeOf ObjEnt Is AcadMText Then

 

*Here I need to select the current object in Autocad*

 

 

Any help would be greatly appreciated.

11 REPLIES 11
Message 2 of 12
Alfred.NESWADBA
in reply to: Markintx

Hi,

 

I guess the question is not clear and that might be the reason for no response.

You ask for:

>> I need to select the current object

And now what is unclear (at least for me):

You have a current object, so what is to select? You already have it, otherwise it would not be currently selected.

 

Describe more from what you try to accomplish, that will make it more clear for us and so the answers will arrive (hopefully).

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 12
Markintx
in reply to: Alfred.NESWADBA

I want to select the current object in the onscreen in autocad,or maybe I want to make a selection set out of the current object would be a better way to put it.

 

My goal is to take each of those objects and use SendCommand to move or modify each one.

 

I hope this clarifies my question more.

 

Thanks again.

Message 4 of 12
Alfred.NESWADBA
in reply to: Markintx

Hi,

 

with VBA and your wish to run that with SendCommand (which I would try to avoid as often as possible) you can use the handle and LISP to make an object selected.

The LISP statement therefor is:

 

      (sssetfirst (setq SELSET (ssadd (handent "287"))) SELSET)

 

That makes the object with handle &h287 selected/gripped.

(whereas "287" is a handle (hex value of a handle), you should take your handle from your routine.)

 

If you want to use move with that single object you can start

     _MOVE

     (handent "287")

 

     0,0,0,

     1,1,1

 

...also with SendCommand.

 

But take care, SendCommand is easy to use in some cases, but generates a lot of conflicts as you don't get feedback (e.g if the user cancels within the command).

 

Good luck, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 12
Markintx
in reply to: Alfred.NESWADBA

Do you know how to select the object in my example from VBA (I dont use LISP)

Answering that would be a great help.

Message 6 of 12
Alfred.NESWADBA
in reply to: Markintx

Hi,

 

with VBA I don't know a way to get a object selected/gripped.

But you can use SendCommand to send the LISP statement 😉

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 7 of 12
Markintx
in reply to: Alfred.NESWADBA

I was able to achieve the desired results using the handet command you suggested

Here is what is I was able to get to work.

 

For Each ObjEnt In ThisDrawing.ModelSpace
 
   If TypeOf ObjEnt Is AcadMText Then
   ObHandle = ObjEnt.Handle
   lisp = "(handent " & Chr(34) & ObHandle & Chr(34) & ")  "
   ThisDrawing.SendCommand ("select ")
   ThisDrawing.SendCommand (lisp)

 

 

Thanks again for the help.

Message 8 of 12
Markintx
in reply to: Markintx

I selected my repost as the answer to make it easier for somone to find exactly what worked.

Thanks to Alfred.NESWADBA for pushing me in the right direction.

Message 9 of 12
gopal.singh
in reply to: Markintx

I also want to do the same action. I tried to make a code based on your solution but failed.

 can you please post your whole code.

thanx

 

Message 10 of 12
Markintx
in reply to: gopal.singh

Here is a simplified version that works.

Open a blank drawing and create 3 or 4 pieces of Mtext

then Step through this code with F8 instead of just running the code and you can watch each step.

It will select each object and then erase it, then select the next one, and erase it.

 

Sub SelectVBA()
Dim ObjEnt As AcadEntity
Dim ObHandle As Variant
Dim lisp As String

For Each ObjEnt In ThisDrawing.ModelSpace
 
   If TypeOf ObjEnt Is AcadMText Then
   ObHandle = ObjEnt.Handle
   lisp = "(handent " & Chr(34) & ObHandle & Chr(34) & ")  "
   ThisDrawing.SendCommand ("select ")
   ThisDrawing.SendCommand (lisp)
   ThisDrawing.SendCommand ("erase ")
End If

Next


End Sub

Message 11 of 12
dobryj.t
in reply to: Markintx

EXcuse me, but after  ThisDrawing.SendCommand ("select ") AutoCAD waits for selecting an object by the user instead of selecting all MText automatically. What is the cause of that? 

Message 12 of 12
Ed.Jobe
in reply to: dobryj.t

Because that's the way the SELECT command works. When you use SendCommand, you can't change the way the command works. You have to use SelectionSet filtering. Here is a post that has some code for filtering for mtext. You can search the forum for other samples.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost