Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Client Graphics - Circle Fill Colour

7 REPLIES 7
Reply
Message 1 of 8
matt_jlt
1152 Views, 7 Replies

Client Graphics - Circle Fill Colour

I am trying to fill a client graphics circle with colour but am having no luck, can anyone help me. I'm just using the sample included in the API Samples.

 

' Create a transient circle object
Dim oCircle As Inventor.Circle
Set oCircle = oTransGeom.CreateCircle(oCenter, oNormal, 1)
        
' Create a circle graphics object within the node.
Dim oCircleGraphics As CurveGraphics
Set oCircleGraphics = oCurvesNode.AddCurveGraphics(oCircle)
        
Call oCircleGraphics.Color.SetColor(255,35,35) ' This line doens't work

 Any help would be great,

cheers. Matt

7 REPLIES 7
Message 2 of 8
nagwani
in reply to: matt_jlt

Hi Matt,

 

Following worked for me.

 

Dim col As Color
Set col = ThisApplication.TransientObjects.CreateColor(255, 35, 35)
oCircleGraphics.Color = col

 

See attached image.

 

Hope this helps!

 

-Ishwar N

 

color.png

Message 3 of 8
matt_jlt
in reply to: nagwani

Thanks, but I'm actually trying to fill the circle with a colour (see image attached) , it seems like i'm doing the wrong thing as you've shown me the colour option only changes the line colour.

 

Any ideas or can it not be done for circles?

 

Thanks, Matt.

Screenshot.jpg

Message 4 of 8
ekinsb
in reply to: matt_jlt

Client graphics are at a very low level in the system.  At the lowest level all they understand is lines, triangles, and textures.  At this level, to create what looks like a filled circles you'll need to create a set of triangles that fills the area of the circle and approximates the outer smooth shape.  That's what Inventor is doing internally.

 

Client graphics does support one level of abstraction that makes it a little bit easier than working with triangles and lines call CurveGraphics.  The sample code shown previously that draws a circle demonstrates this.  In this case the circle geometry is being provided to client graphics and then it's internally converting it to a series of lines for the display.  It approximates it with enough lines based on the current size of the circle on the screen so that it always appears smooth.  There currently isn't the concept of filling curves in client graphics.  However, you can effectively do the same thing by providing a surface instead.  This is called SurfaceGraphics and you can provide a solid or surfaces and it will create the appropriate triangles for the display.  In this case we want a surface that is a plane trimmed by a circle.  The API supports to the ability to create transient surfaces, but I'll be honest and tell you it is one of the more advanced areas of the API.  Here's some sample code that demonstrates creating what looks like a filled circle.

 

Public Sub FilledCircle()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    
    Dim cg As ClientGraphics
    On Error Resume Next
    Set cg = partDoc.ComponentDefinition.ClientGraphicsCollection.Item("Fill Test")
    If Err.Number = 0 Then
        ' Client graphics already exist, so delete them.
        cg.Delete
        ThisApplication.ActiveView.Update
        Exit Sub
    End If
    
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry
    
    Dim tBRep As TransientBRep
    Set tBRep = ThisApplication.TransientBRep
    
    Dim bodyDef As SurfaceBodyDefinition
    Set bodyDef = tBRep.CreateSurfaceBodyDefinition
    
    Dim lumpDef As LumpDefinition
    Set lumpDef = bodyDef.LumpDefinitions.Add
    
    Dim shellDef As FaceShellDefinition
    Set shellDef = lumpDef.FaceShellDefinitions.Add
    
    Dim pl As Plane
    Set pl = tg.CreatePlane(tg.CreatePoint(0, 0, 0), tg.CreateVector(0, 0, 1))
    
    Dim faceDef As FaceDefinition
    Set faceDef = shellDef.FaceDefinitions.Add(pl, False)
    
    Dim circ As Inventor.Circle
    Set circ = tg.CreateCircle(tg.CreatePoint(0, 0, 0), tg.CreateUnitVector(0, 0, 1), 3)
    
    Dim vert1 As VertexDefinition
    Set vert1 = bodyDef.VertexDefinitions.Add(tg.CreatePoint(3, 0, 0))
    
    Dim vert2 As VertexDefinition
    Set vert2 = bodyDef.VertexDefinitions.Add(tg.CreatePoint(3, 0, 0))
    
    Dim edgeDef As EdgeDefinition
    Set edgeDef = bodyDef.EdgeDefinitions.Add(vert1, vert2, circ)
    
    Dim loopDef As EdgeLoopDefinition
    Set loopDef = faceDef.EdgeLoopDefinitions.Add
    
    Call loopDef.EdgeUseDefinitions.Add(edgeDef, False)
    
    Dim body As SurfaceBody
    Dim errors As NameValueMap
    Set body = bodyDef.CreateTransientSurfaceBody(errors)
    
    Set cg = partDoc.ComponentDefinition.ClientGraphicsCollection.Add("Fill Test")
    
    Dim node As GraphicsNode
    Set node = cg.AddNode(1)
    
    Dim surfGraphics As SurfaceGraphics
    Set surfGraphics = node.AddSurfaceGraphics(body)
    surfGraphics.Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0, 1)
    
    ThisApplication.ActiveView.Update
End Sub

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 5 of 8
ekinsb
in reply to: ekinsb

Does anyone know the trick to posting code so the blank lines aren't taken out?


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 6 of 8
matt_jlt
in reply to: ekinsb

Hi Brian, thanks for your response. You're right, it is complex Smiley Happy more so than i anticipated to achieve the circle, but i like a good challenge. Would there be any drawbacks using this method compared to triangle fans with regard to performance?

 

Regards, Matt.

Message 7 of 8
ekinsb
in reply to: matt_jlt

I don't know for sure about performance of creating surface graphics vs. triangle graphics.  It would take some timing tests to see how much difference there is.  It's only a concern if you're creating a lot of the circles.  In that case you can create the first one using surface graphics and then create the others by copying the graphics node and positioning it where the new circle needs to be.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 8 of 8
nagwani
in reply to: matt_jlt

Hi Matt,

 

The subroutine below can be used for drawing circle as traingle fans. 

 

Hope this helps!

 

-Ishwar N

Public Sub AddCircleGraphics( _
    ByVal oDoc As Document, _
    ByVal oCircleNode As GraphicsNode, _
    ByVal cpt As Point, _
    ByVal norm As UnitVector, _
    ByVal dRadius As Double)
    
    
    Dim oDataSets As GraphicsDataSets
    If oDoc.GraphicsDataSetsCollection.Count > 0 Then
        oDoc.GraphicsDataSetsCollection.Item(1).Delete
    End If

    Set oDataSets = ThisApplication.ActiveDocument.GraphicsDataSetsCollection.Add(oCircleNode.Parent.ClientId)
    
    ' Create a coordinate set.
    Dim oCoordSet As GraphicsCoordinateSet
    Set oCoordSet = oDataSets.CreateCoordinateSet(1)

    ' No of sides to approximate the circle
    Dim iSideCount As Long
    iSideCount = 100
    

    
    ' Create an array containing points to used in drawing the circle as trainglefan graphics.
     
     Dim adPointCoords() As Double
     ReDim adPointCoords(1 To (iSideCount + 1) * 3) As Double
     Dim i As Long
     Dim dAngle As Double
     
  
     ' Define the points for the outline of the Circle.
     dAngle = 0
     For i = 0 To iSideCount - 1
         adPointCoords(i * 3 + 1) = cpt.X + dRadius * Cos(dAngle)
         adPointCoords(i * 3 + 2) = cpt.Y + dRadius * Sin(dAngle)
         adPointCoords(i * 3 + 3) = 0
         
         ' Increment the angle for the next point
         dAngle = dAngle + ((2 * 3.14159265358979) / iSideCount)
     Next
     
  
     ' Define coordinate at the center
      adPointCoords(iSideCount * 3 + 1) = cpt.X
      adPointCoords(iSideCount * 3 + 2) = cpt.Y
      adPointCoords(iSideCount * 3 + 3) = 0
 
     ' Assign the points to the coordinate set.
     Call oCoordSet.PutCoordinates(adPointCoords)
     
     ' Create a triangle fan
     Dim oCircleTriangleFan As TriangleFanGraphics
     Set oCircleTriangleFan = oCircleNode.AddTriangleFanGraphics

 
     ' Set the coordinates and Color
     oCircleTriangleFan.CoordinateSet = oCoordSet
     oCircleNode.RenderStyle = ThisApplication.ActiveDocument.RenderStyles.Item("Red")
     
     ' update the display to see the results.
     ThisApplication.ActiveView.Update
     
     ' Define a normal for circle.
     Dim oCircleNormals As GraphicsNormalSet
     Set oCircleNormals = oDataSets.CreateNormalSet(1)
     Call oCircleNormals.Add(1, norm)
     
     ' Assign the normals to the circle.
     oCircleTriangleFan.NormalSet = oCircleNormals
   
    
     ' update the display to see the results.
     ThisApplication.ActiveView.Update

End Sub

 

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

Post to forums  

Autodesk Design & Make Report