Selecting object, changing color, using form

Selecting object, changing color, using form

Anonymous
Not applicable
739 Views
2 Replies
Message 1 of 3

Selecting object, changing color, using form

Anonymous
Not applicable
I am trying to write a VBA macro that, when I select a polyline, and then click a shortcut key (or cmd button on a form) will simply change the color of the polyline.

When I say selected, I mean highlighted with the blue boxes. (gripped?)
The following code does that, but it only works when I have the VBA editor open. When I close the editor and bind the macro to a keyboard shortcut, it doesnt work.

I would really like some code that does this:
Show form->click button to start process-> hide form->user selects a polyline->color is changed->Form reappears...or something along those lines.


Code so far:

Sub change_color_sel()
Dim pfSS As AcadSelectionSet
Dim ssobject As AutoCAD.AcadObject

'Dim ssobject As AcadEntity
Set pfSS = ThisDrawing.PickfirstSelectionSet
For Each ssobject In pfSS
ssobject.color = 2
ssobject.Update
Next
End Sub


-----
Thanks in advance
0 Likes
740 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
In general, there's no reason a routine wouldn't work when bound to a
shortcut if it works in ide

however, the phrase "doesn't work" doesn't help us help you 🙂

you should trap all possible errors and include Debug.Print calls or log
events to file etc throughout any prog to see what's taking place, and where
something bombs, then get the Err.Description when it bombs so you know why
it "doesn't work"

sadly, ThisDrawing.PickfirstSelectionSet is extremely buggy...you always
have to test the return

even if you weren't using that method, you always "should" test a selection
set(or any other object for that matter) before using it...
if obj is Nothing then....problem
if obj.Count = 0 then... problem
else
'ok to proceed


depending on your version...
objects no longer have .Color property... it's .TrueColor

I would really like some code that does this:
Show form->click button to start process-> hide form->user selects a
polyline->color is changed->Form reappears...or something along those lines.

if you want to use a form, create a user form, put a command button on it
(name it cmd1 for instance)

Then in
Sub Cmd1_Click ()
Me.Hide
change_color_sel
Me.Show
End Sub

create a module to fire up the form
Sub Main()
dim f as MyUserForm
Set f = New MyUserForm
f.Show
Set f = Nothing
End sub


caveat to using a form in this way...
you have to use vl-vbarun to call your macro to allow you to pick on screen
while a userform is hidden
;;; however, it will crash the macro if toolbar button is clicked
;;; AND IT WILL CANCEL PICKFIRST SELECTION SET

hth
Mark


Sub change_color_sel()
On Error GoTo ErrCtl

Dim pfSS As AcadSelectionSet
'Dim ssobject As AcadObject

'this is fine since a sel set can only contain entities...
Dim ssobject As AcadEntity
Set pfSS = ThisDrawing.PickfirstSelectionSet

'>>>> test pfSS before using....

For Each ssobject In pfSS
ssobject.color = 2
ssobject.Update
Next

Exit Sub

ErrCtl:

'tells user about error but msg is volatile(gone after closing msg box)
MsgBox "Error occurred " & Err.Description

'msg less volatile but have to open ide to look in Immediate window
Debug.Print "Error occurred " & Err.Description

'non volatile...save to text file or whereever for permanent record
LogError "Error occurred " & Err.Description

Err.Clear
On Error GoTo 0
End Sub
0 Likes
Message 3 of 3

Anonymous
Not applicable
> depending on your version...
> objects no longer have .Color property... it's .TrueColor

Has it actually gone from '08?

In '07 .color can still be used but it didn't list in intellisense.

Dave F.
0 Likes