Conditionally change the color of the sketch entity

Conditionally change the color of the sketch entity

sawaani
Enthusiast Enthusiast
503 Views
2 Replies
Message 1 of 3

Conditionally change the color of the sketch entity

sawaani
Enthusiast
Enthusiast

 

For example, red is green, green is blue ...
The code below will change everything.
Apparently, the color comparison judgment is not good.

Is there any way?

 

Sub test()
Dim oSketch As PlanarSketch
Set oSketch = ThisApplication.ActiveEditObject

Dim oSketchEntity As SketchEntity

Dim oColorRed As Color
Set oColorRed = ThisApplication.TransientObjects.CreateColor(255, 0, 0) 'Red

Dim oColorGreen As Color
Set oColorGreen = ThisApplication.TransientObjects.CreateColor(0, 255, 0) 'Green

For Each oSketchEntity In oSketch.SketchEntities
If Not TypeOf oSketchEntity Is SketchPoint Then
If oSketchEntity.OverrideColor = oColorRed Then
oSketchEntity.OverrideColor = oColorGreen
End If
End If
Next
End Sub

0 Likes
504 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor

I think you need to check the color red, green and blue values for a good comparison. check the function "IsSameColor" in the iLogic rule below. Also if the color was never set before then the property "OverrideColor" will return "Nothing" you need to check for that.

I also discovered that you need to cast the sketch entity to the real entity type to be able to update the color.  check the function "SetColorToSketchEntity"

Last thing to note I used iLogic instead of VBa. This is because I think VBa is obsolete and you should not use it anymore if possible. (Check my blog post on that topic!)

 

Sub Main()
	Dim oSketch As PlanarSketch = ThisApplication.ActiveEditObject
	Dim oColorRed As Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0) 
	Dim oColorGreen As Color = ThisApplication.TransientObjects.CreateColor(0, 255, 0) 

	For Each oSketchEntity As SketchEntity In oSketch.SketchEntities
		If TypeOf oSketchEntity Is SketchPoint Then Continue For

		If oSketchEntity.OverrideColor Is Nothing Then
			' SketchEntity color was never set. Set it to red as initial value
			SetColorToSketchEntity(oSketchEntity, oColorRed)
		Else

			Dim color As Color = oSketchEntity.OverrideColor
			If IsSameColor(color, oColorRed) Then
				SetColorToSketchEntity(oSketchEntity, oColorGreen)
			End If
		End If
	Next
End Sub

Public Function IsSameColor(color1 As Color, color2 As Color) As Boolean
	Return (color1.Red = color2.Red AndAlso color1.Green = color2.Green AndAlso color1.Blue = color2.Blue)
End Function

Public Sub SetColorToSketchEntity(sketchEntity As SketchEntity, color As Color)

	Select Case sketchEntity.Type
		Case ObjectTypeEnum.kSketchLineObject
			Dim se As SketchLine = sketchEntity
			se.OverrideColor = color
		Case ObjectTypeEnum.kSketchCircleObject
			Dim se As SketchCircle = sketchEntity
			se.OverrideColor = color
		Case ObjectTypeEnum.kSketchArcObject
			Dim se As SketchArc = sketchEntity
			se.OverrideColor = color
		Case Else
			' this example does not include all types:
			' SketchArc, SketchCircle, SketchControlPointSpline, SketchEllipse,
			' SketchEllipticalArc, SketchEquationCurve, SketchFixedSpline,
			' SketchLine, SketchOffsetSpline, SketchPoint, SketchSpline
			MsgBox("Unable to set color of sketch Entity of type: " & sketchEntity.Type)
	End Select

End Sub

 

Jelte de Jong
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.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 3

sawaani
Enthusiast
Enthusiast

Thank you for answering.

 

There were two tips.
It is necessary to compare the RGB values respectively.
(I came up with this, but asked if there was a smarter way)

The other is the need for typecasting when changing colors.

 

I don't really understand this yet. If I simply change the color with VBA code, it works by specifying it directly to the entity type.

 

I'll try a little more with VBA and ask again.
(ilogic doesn't really get used to it)

0 Likes