Highlighting or selecting elements from vba code

Highlighting or selecting elements from vba code

ofcarvajall
Participant Participant
6,574 Views
7 Replies
Message 1 of 8

Highlighting or selecting elements from vba code

ofcarvajall
Participant
Participant

Hello,

 

I am working with AutoCad 2022 and with Excel VBA. In my code, I read in a Excel Worksheet the info that I need from the drawing including the Handle of the Entities I am interested on.

 

Next step, to verify some Acad Drawing info based on Excel calculations. For this, I pick the Entities Handle ID on Excel and it highlights the Entity on Acad. Some times Entities highlighting doesn't do enough contrast to differentiate the element of my interest between all the other objects and it is hard to see it.

 

Now I think It would be better if the entity I need to verify get selected, as it is made in Acad environment with the mouse. Unfortunately, the practical way to verify the info is accessing by the Handle ID.

 

I have looked for alternatives in Internet and found something regarding with SelectionSets, but result was not different of just highlight the entity.

 

Any suggestion to select (as with the mouse) or to improve the colors or highlighting characteristics of the entities?

 

I attach some pics how is my highlighted objects showing up.

 

The code I am using is:

 

Sub dfSelHnd()

Dim actldwg As AcadDocument
Dim tAr(0) As AcadEntity 'Add items to selectionset must be done with arrays

Dim rng As Range
Dim txt As String

''---
''---
''---

Set rng = Selection
Set actldwg = AutoCAD.Application.ActiveDocument

txt = rng.Value '' txt is the HandleID

Set tAr(0) = actldwg.HandleToObject(txt)

Call zoomit(actldwg, tAr(0))
tAr(0).Highlight (True)

End Sub'''

0 Likes
Accepted solutions (1)
6,575 Views
7 Replies
Replies (7)
Message 2 of 8

norman.yuan
Mentor
Mentor
Accepted solution

If I understand your question correctly, what you want is provide user with visual hint of which entity (or entities) is/are the target(s) of the code execution while the code execution is waiting for user interaction (if the code execution does not need to wait for user interaction, then there is no point to provide visual hint to user, right?).

 

With VBA/COM API, your option is quite limited. Basically you just use Entity.Highlight(True/False) method. As for SelectionSet, it is just a entity container and is different from the action when user manually select entities in AutoCAD editor. Once your code creates a SelectionSet, the look of the "selected" (by code) entities do not change (do not get highlighted), unless you call HIghlight() on each entity in the SelectionSet.

 

If you are not satisfied with the visual effect of Highlight() method, you might consider to TEMPORARILY change the entity's color, lineweight (you have to enable AutoCAD to show lineweight, of course) to make it more eye-catching. Or you can even drawing something temporarily, like the entity's bounding box, a circle around it... Well, you have to make sure to reverse the temporary change back when the code execution no longer need to prompt user for the target entities.

 

With AutoCAD .NET API, there are more options you could choose without having to change entity's properties (color, lineweight) or draw temporary entities. You can overrule the regular highlight to whatever visual effects you like; or you could also use DrawableOverrule, TransientGraphics... 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 8

ofcarvajall
Participant
Participant

Hello Norman,

 

I appreciate your help and your reply. As you mention, my intention is to give the user a visual hint to know which entity is working with and to allow user to complete in excel the info that it is needed. The main code goes entity by entity, highlighting each one and giving time for the user interaction.

 

I am using some of the solutions you indicate, I am drawing a circle. Other option I found was to change Selection effect color (right click on Acad Screen, selection tab, selection effect color). It doesn't work always, depending on the entity original color but made the interaction with the user easier. 

 

I understand from your answer that form VBA is not possible to select (as it is possible to do with the mouse over an entity, picking it on, an getting blue highlighting and showing up blue squares). Is there a way to "select" the entity as it is possible with the mouse?

 

I am pretty new in Acad Vba, I will also try with AutoCAD .NET API.

 

Many thanks,

 

Regards,

Oscar

0 Likes
Message 4 of 8

reuter.philipp
Enthusiast
Enthusiast

Hello,

I wanted to ask a similar question, so I'll put it here.

Is it somehow possible to select objects through VBA so that one can then apply a command via the command line? It is about the command shrinkwrap from AutoCAD architecture, which, as far as I know, does not exist as a VBA command. But I only want to select objects with different parameters and layer names. I get that also in a SelectionSet, but as you wrote above, it is a different one than in the AutoCAD GUI and cannot be selected from there. Does anyone know a solution?

 

With kind regards,

Philipp

0 Likes
Message 5 of 8

Ed__Jobe
Mentor
Mentor

Since the command line can interpret lisp, you have to get the lisp version of the handle and format it as lisp and pass that as an argument to the command being entered with SendCommand. I wrote a function to assist with that. Some commands will accept a selectionset as the argument. You'll have to try it with the arch command you want.

 

Public Function Ent2lspEnt(entObj As AcadEntity) As String
    'Designed to work with SendCommand, which can't pass objects.
    'This gets an objects handle and converts it to a string
    'of lisp commands that returns an entity name when run in SendCommand.
    Dim entHandle As String
    
    entHandle = entObj.Handle
    Ent2lspEnt = "(handent " & Chr(34) & entHandle & Chr(34) & ")"
End Function

 

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

0 Likes
Message 6 of 8

reuter.philipp
Enthusiast
Enthusiast

Thanks, I will test that, but also had another idea because I found a similar question in the forum. I wanted to try to create a named group with it, which should then be selectable from the command line. But the code is not tested yet.

I just saw that you had commented there too 😉

https://forums.autodesk.com/t5/vba/selecting-objects-on-the-model-from-selectionset-created-in-vba/t...

 

Public Sub SSToGroup(SelectionSetName as String, ss As AcadSelectionSet)
    'Set ss = ThisDrawing.SelectionSets.Add(SelectionSetName)
    ss.Select acSelectionSetAll
    ss.Delete
    ThisDrawing.SendCommand "_SELECT" & vbCr & "_PREVIOUS" & vbCr
    ThisDrawing.SendCommand "-GROUP" & vbCr & "Test" & vbCr
End Sub

 

With kind regards,

Philipp

0 Likes
Message 7 of 8

iamzain16
Observer
Observer

You can select entities in VBA by select group.

 ThisDrawing.SendCommand ("_SELECT" + vbCr + "G" + vbCr + GroupName + vbCr + vbCr)

So first You need to create selectionset, then make a group from selectionset. Full sample would be: Jbbattery Russia

Public Sub Test()
    Dim ssh As AcadSelectionSet
    Dim Ftyp(1) As Integer
    Dim Fdat(1) As Variant
    Dim BlockName As String
    BlockName = "A-1"
    Dim F1, F2 As Variant
    Ftyp(0) = 0: Fdat(0) = "Insert"
    Ftyp(1) = 2: Fdat(1) = BlockName
    Set sstest = ThisDrawing.SelectionSets.Add("sstest")
    F1 = Ftyp
    F2 = Fdat
    sstest.Select acSelectionSetAll, , , Ftyp, Fdat
     
    Dim GroupName As String
    GroupName = "sstest"

    Dim group As AcadGroup
    Set group = ThisDrawing.Groups.Add(GroupName)
    For Each Item In sstest
        group.AppendItems (Item)
    Next
    sstest.Delete
    ThisDrawing.SendCommand ("_SELECT" + vbCr + "G" + vbCr + GroupName + vbCr + vbCr)
    group.Delete
End Sub

This sample code mark selected blocks (by name). Now You should change the way of selection. But it's not the question, so I hope You will handle it easy.

Message 8 of 8

Ed__Jobe
Mentor
Mentor

I wouldn't go about creating a GROUP object just to be able to select entities, unless you are already using groups.

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

0 Likes