How to add a primitive sphere in sketch vb.net

How to add a primitive sphere in sketch vb.net

Anonymous
Not applicable
1,589 Views
10 Replies
Message 1 of 11

How to add a primitive sphere in sketch vb.net

Anonymous
Not applicable

Hello! I have a problem, I'm trying to draw a primitive sphere in a sketch, but I don't know how to do it, I tried drawing some sphere using clientgraphic object and yes, it can draw spheres but I cannot edit them.

 

Im trying to draw an circle and then use revolve to make the sphere. but I don't know how to move it. I hope someone can help me.

0 Likes
Accepted solutions (1)
1,590 Views
10 Replies
Replies (10)
Message 2 of 11

ekinsb
Alumni
Alumni

The API doesn't provide a simple equivalent to the primitive creation commands you see in the user interface.  However, those commands just simplify the create of a sketch and feature so you can get the same results using the API.  Sketches are 2D so it doesn't make sense to say you want to create a sphere in a sketch.  However, we can create a semi-circle that we then use as input for a revolve feature to create a 3D sphere.  The code below demonstrates this and ends up creating the same result as the sphere primitive command.

 

Public Sub CreateRevolution()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    Dim partDef As PartComponentDefinition
    Set partDef = partDoc.ComponentDefinition
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry
    
    ' Create a new sketch on the X-Y base plane.
    Dim sketch As PlanarSketch
    Set sketch = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))
    
    ' Draw a circle.
    Dim radius As Double
    radius = 5
    Dim circ As SketchCircle
    Set circ = sketch.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(0, 0), radius)
    
    ' Draw a center line through the circle.
    Dim lin As SketchLine
    Set lin = sketch.SketchLines.AddByTwoPoints(tg.CreatePoint2d(-radius, 0), tg.CreatePoint2d(radius, 0))
    lin.Centerline = True
    
    ' Constrain the line to circle.
    Call sketch.GeometricConstraints.AddCoincident(circ.CenterSketchPoint, lin)
    Call sketch.GeometricConstraints.AddCoincident(lin.StartSketchPoint, circ)
    Call sketch.GeometricConstraints.AddCoincident(lin.EndSketchPoint, circ)
    
    ' Create a profile from the sketch.
    Dim prof As Profile
    Set prof = sketch.Profiles.AddForSolid()
    
    ' Two paths were created in the profile but we only need one (half of the circle)
    ' so delete one of the paths.
    prof.Item(1).Delete
    
    ' Create a revolve feature.
    Dim revolve As RevolveFeature
    Set revolve = partDef.Features.RevolveFeatures.AddFull(prof, lin, kNewBodyOperation)
End Sub

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 11

Anonymous
Not applicable

In fact, I did something like that. I used arcs, and it works, but I want to move that revolve. I want to put it on a point (x,y,z). and planarskets is only 2Dpoints. There is a way to move the sphere after drawing? Thank you for your help. Smiley Wink

0 Likes
Message 4 of 11

ekinsb
Alumni
Alumni

To move the sphere within the part, there are a couple of options, probably neither one being ideal.  The first is to edit the skectch and change the position of the circle within the sketch.  This is limiting because you can only move the circle on the sketch plane, not anywhere in space.  It's also possible to redefine the sketch to change which plane it is based on but that's very practical either if you need to position the sphere anywhere in 3D space.

 

The second approach is to use the Move feature and move the spherical body that was created.  This allows you to reposition it at any location and you can control that position afterword by using three parameters that are created with the feature to control the X, Y, and Z offset.

 

The typical approach to move things around is to create each thing as an individual part and then reposition them within an assembly.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 5 of 11

Anonymous
Not applicable

Yes, I had already tried to use the MoveFeatures. But each sphere I make is saved area(?) called solidbodies in Inventor.  When I use MoveFeatures, I cannot move the revolve, I move the solidbodie with all those spheres within. I have this code.

Dim oRevFeature As RevolveFeature
                    oRevFeature = doc.ComponentDefinition.Features.RevolveFeatures.AddFull _
                        (oProfile, oLine, PartFeatureOperationEnum.kJoinOperation)
                    

                    ' Create a collection containing the body to move.
                    Dim bodyCollection As ObjectCollection
                    bodyCollection = _InventorApp. _
                          TransientObjects.CreateObjectCollection
                    bodyCollection.Add(oRevFeature.SurfaceBodies.Item(1))

                    Dim moveDef As MoveDefinition
                    moveDef = oDef.Features.MoveFeatures.CreateMoveDefinition(bodyCollection)
                    moveDef.AddFreeDrag(point.X, point.Y, point.Z)

                    Dim move As MoveFeatures
                    move = oDef.Features.MoveFeatures.Add(moveDef)

 

I don't know How can I use MoveFeatures to move revolves and not bodies.

0 Likes
Message 6 of 11

ekinsb
Alumni
Alumni
Accepted solution

The problem is that you're using the Join operation when you create each sphere so it's joining that sphere to the existing sphere(s).  You need to be creating a new body for each sphere.  Here's a modified version of the previous program that does that.  I've created a function where I can pass in the position and radius and it will create a sphere at that location.  It also modifies the names of the parameters so it's easy to find which parameters control the position of which sphere.  By changing the parameters you can reposition any sphere at any location after it's been created.

 

Public Sub CreateRevolutions()
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry
    
    Dim points(5) As Point
    Set points(0) = tg.CreatePoint(4, 0, 0)
    Set points(1) = tg.CreatePoint(0, -4, 0)
    Set points(2) = tg.CreatePoint(0, 4, 0)
    Set points(3) = tg.CreatePoint(4, 4, 0)
    Set points(4) = tg.CreatePoint(-4, 4, 0)
    
    Dim moves(5) As MoveFeature
    Dim i As Integer
    For i = 0 To 4
        Set moves(i) = CreateSphere(1.5, points(i))
    Next
End Sub


Public Function CreateSphere(Radius As Double, Position As Point) As MoveFeature
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    Dim partDef As PartComponentDefinition
    Set partDef = partDoc.ComponentDefinition
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry
    
    ' Create a new sketch on the X-Y base plane.
    Dim sketch As PlanarSketch
    Set sketch = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))
    
    ' Draw a circle.
    Dim circ As SketchCircle
    Set circ = sketch.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(0, 0), Radius)
    
    ' Draw a center line through the circle.
    Dim lin As SketchLine
    Set lin = sketch.SketchLines.AddByTwoPoints(tg.CreatePoint2d(-Radius, 0), tg.CreatePoint2d(Radius, 0))
    lin.Centerline = True
    
    ' Constrain the line to circle.
    Call sketch.GeometricConstraints.AddCoincident(circ.CenterSketchPoint, lin)
    Call sketch.GeometricConstraints.AddCoincident(lin.StartSketchPoint, circ)
    Call sketch.GeometricConstraints.AddCoincident(lin.EndSketchPoint, circ)
    
    ' Create a profile from the sketch.
    Dim prof As Profile
    Set prof = sketch.Profiles.AddForSolid()
    
    ' Two paths were created in the profile but we only need one (half of the circle)
    ' so delete one of the paths.
    prof.Item(1).Delete
    
    ' Create a revolve feature.
    Dim revolve As RevolveFeature
    Set revolve = partDef.Features.RevolveFeatures.AddFull(prof, lin, kNewBodyOperation)
    
    ' Move it to the specified position.
    Dim objColl As ObjectCollection
    Set objColl = ThisApplication.TransientObjects.CreateObjectCollection
    Call objColl.Add(revolve.Faces.Item(1).SurfaceBody)
    Dim moveDef As MoveDefinition
    Set moveDef = partDef.Features.MoveFeatures.CreateMoveDefinition(objColl)
    Call moveDef.AddFreeDrag(Position.x, Position.y, Position.Z)
    Set CreateSphere = partDef.Features.MoveFeatures.Add(moveDef)
    
    ' Rename the parameters to they're easily identified in the parameters dialog.
    Dim freeDrag As FreeDragMoveOperation
    Set freeDrag = CreateSphere.Definition.MoveOperation(1)
    Dim name As String
    name = CreateSphere.name & " X"
    name = Replace(name, " ", "_")
    freeDrag.XOffset.name = name
    
    name = CreateSphere.name & " Y"
    name = Replace(name, " ", "_")
    freeDrag.YOffset.name = name
    
    name = CreateSphere.name & " Z"
    name = Replace(name, " ", "_")
    freeDrag.ZOffset.name = name
End Function

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 7 of 11

Anonymous
Not applicable

Thank you sir! That works!! I needed to use kNewBodyOperation no create a new solid body and then can move it! You have saved me.

0 Likes
Message 8 of 11

Anonymous
Not applicable

Sir, Thank you for the help. I need to update the radius of the sphere instead of creating new. Is there any way to do so?. Basically am providing radius value from a form to an active part document.When ever i change the radius, am able to see new solids and new sketches in my part document. 

0 Likes
Message 9 of 11

Anonymous
Not applicable

you mean, updating the radius in runtime? 

0 Likes
Message 10 of 11

Anonymous
Not applicable
Yes, I have a VB.form developed using VB.net. while running my code.i will create a new sphere. In the same form I have a update to enter new value.
0 Likes
Message 11 of 11

Anonymous
Not applicable

Hey there, it's been a while since the last time I logged in, did you find out how to solve your problem? 

0 Likes