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: 

Automated Create sketch blocks

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
robmatthews
1241 Views, 2 Replies

Automated Create sketch blocks

Hey there.

 

Can someone please give me a couple of pointers in creating a named sketch block (in an ipt) containing entities that I have created in the same sub?

Oh, and I'm doing this from Excel, in Inventor 2010.

 

What I have:

 

Function CreateUncutTagSketchBlock()
Dim IVApp As Inventor.Application
Dim oDoc As Document
Dim oCompDef As ComponentDefinition
Dim oSketch As Sketch
Dim oTG As TransientGeometry
Dim FileName As String
Dim Coords(1 To 10) As Point2d
Dim oSketchCircle As Circle2d
Dim oSketchArcs(1 To 2) As Arc2d
Dim oSketchLines(1 To 2) As SketchLine
Dim oSketchBlockDef As SketchBlockDefinition

Dim oArc As SketchEntity
Dim oLine As SketchEntity
Dim oCirc As SketchEntity

Dim dRadius As Double
Dim icounter As Integer
Dim oSketchObjects As ObjectCollection
Set IVApp = GetObject(, "Inventor.Application")
Set oDoc = IVApp.ActiveDocument
FileName = oDoc.FullFileName

dRadius = 2.5

'On Error Resume Next
If oDoc.DocumentType = kPartDocumentObject Then
    'If oDoc.SketchActive = True Then 'check to ensure you have a sketch active
        Set oCompDef = oDoc.ComponentDefinition ' Set a reference to the component definition.
        Set oSketch = oCompDef.Sketches.Item(oCompDef.Sketches.Count)
        'Set oSketchBlockDef = oDoc.ComponentDefinition.SketchBlockDefinitions.Add("UncutTag") ', oSketchObjects)
        Set oTG = IVApp.TransientGeometry
        Set Coords(1) = oTG.CreatePoint2d(0, 0) 'Circle centre  (All co-ordinates in cm)
        Set Coords(2) = oTG.CreatePoint2d(0, 2.5) 'point on circle circumference
        Set Coords(3) = oTG.CreatePoint2d(-0.1, 2.2) ' Cutout: top left
        Set Coords(4) = oTG.CreatePoint2d(0.1, 2.2) ' Cutout: top right
        Set Coords(5) = oTG.CreatePoint2d(0.1, 1.8) ' Cutout: bottom right
        Set Coords(6) = oTG.CreatePoint2d(-0.1, 1.8) ' Cutout: bottom left
        Set Coords(7) = oTG.CreatePoint2d(-0.1, 2) ' Cutout: Left Arc centre
        Set Coords(8) = oTG.CreatePoint2d(0.1, 2) ' Cutout: Right Arc centre
        Set oTG = IVApp.TransientGeometry
        Call oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(0, 0), dRadius)
        With oSketch.SketchLines
            Set oSketchLines(1) = .AddByTwoPoints(Coords(3), Coords(4))
            Set oSketchLines(2) = .AddByTwoPoints(Coords(5), Coords(6))
        End With
        Call oSketch.SketchArcs.AddByCenterStartEndPoint(Coords(7), Coords(3), Coords(6), True)
        Call oSketch.SketchArcs.AddByCenterStartEndPoint(Coords(8), Coords(4), Coords(5), False)
        
        Set oSketchObjects = IVApp.TransientObjects.CreateObjectCollection
        For Each oArc In oSketch.SketchArcs
            Call oSketchObjects.Add(oArc)
        Next
        For Each oLine In oSketch.SketchLines
            Call oSketchObjects.Add(oLine)
        Next
        For Each oCirc In oSketch.SketchCircles
            Call oSketchObjects.Add(oCirc)
        Next
        
        Call oSketch.SketchBlocks("UncutTag").Add(oSketchObjects)
        Call oDoc.ComponentDefinition.SketchBlockDefinitions.Add("UncutTag", oSketchObjects)
        'Call oSketch.SketchBlocks.AddByDefinition("UncutTag", oSketchObjects)
        
        ' test insertion/placement
        
    'End If
End If

Set IVApp = Nothing
Set oDoc = Nothing
End Function

 Please note that i've also taken a shortcut with the arcs, and the circle. I did try to create them using the same naming convention as the lines, but... I failed to implement it successfully.

 

Also, I can not quite figure out how to constrain all the start/end points together.

 

Any help you can give me will be greatly appreciated.

 

Rob.

=============================================
This is my signature, not part of my post.
2 REPLIES 2
Message 2 of 3

Hi Rob,

 

Below is a VBA sample that illustrates how to work with sketch blocks.

 

For your second questions, concerning the coincident constraints, the easiest way is to create SketchPoints out of the transient Point2d, then use those as input when you create sketch entities instead of passing directly the transient points. That way Inventor will automatically create a coincident constraint between the sketch entities, because they share the same SketchPoint object.

 

Public Sub CreateBlocks(doc As PartDocument)

    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry

    'Create 1st sketch block definition.
    Dim def1 As SketchBlockDefinition
    Set def1 = doc.ComponentDefinition.SketchBlockDefinitions.Add("Block1")

    'Draw the shape of this part as sketch geometry, (a rectangle with circles at the ends).
    Call def1.SketchLines.AddAsTwoPointRectangle(tg.CreatePoint2d(0, 0), tg.CreatePoint2d(12, 3))
    Call def1.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(1.5, 1.5), 0.5)
    Call def1.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(10.5, 1.5), 0.5)

    'Create 2nd sketch block definition.
    Dim def2 As SketchBlockDefinition
    Set def2 = doc.ComponentDefinition.SketchBlockDefinitions.Add("Block2")

    'Draw the shape of this part as sketch geometry, (a square, with a circle in the middle).
    Call def2.SketchLines.AddAsTwoPointRectangle(tg.CreatePoint2d(0, 0), tg.CreatePoint2d(4, 4))
    Call def2.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(2, 2), 0.5)

End Sub


Public Sub DemoSketchBlocks()

    Dim doc As PartDocument
    Set doc = ThisApplication.ActiveDocument
    
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry

    CreateBlocks doc
    
    'Create a new part sketch based on XY Workplane
    Dim sketch As PlanarSketch
    Set sketch = doc.ComponentDefinition.Sketches.Add( _
        doc.ComponentDefinition.WorkPlanes(3))
    
    Dim def1 As SketchBlockDefinition
    Set def1 = doc.ComponentDefinition.SketchBlockDefinitions("Block1")

    Dim def2 As SketchBlockDefinition
    Set def2 = doc.ComponentDefinition.SketchBlockDefinitions("Block2")

    'Add one instance of Block1 and 2 instances of Block2
    Dim blk1 As SketchBlock
    Set blk1 = sketch.SketchBlocks.AddByDefinition( _
        def1, tg.CreatePoint2d(0, 0))
    
    Dim blk2 As SketchBlock
    Set blk2 = sketch.SketchBlocks.AddByDefinition( _
        def2, tg.CreatePoint2d(2, 5))
    
    Dim blk3 As SketchBlock
    Set blk3 = sketch.SketchBlocks.AddByDefinition( _
        def2, tg.CreatePoint2d(10, 5))

    Dim baseCircle1 As SketchCircle
    Set baseCircle1 = blk1.GetObject(def1.SketchCircles(1))
    
    Dim baseCircle2 As SketchCircle
    Set baseCircle2 = blk1.GetObject(def1.SketchCircles(2))

    Dim square1Circle As SketchCircle
    Set square1Circle = blk2.GetObject(def2.SketchCircles(1))
    
    Dim square2Circle As SketchCircle
    Set square2Circle = blk3.GetObject(def2.SketchCircles(1))

    'Add coincident constraints between the circle center points.
    Call sketch.GeometricConstraints.AddCoincident( _
        baseCircle1.CenterSketchPoint, square1Circle.CenterSketchPoint)
        
    Call sketch.GeometricConstraints.AddCoincident( _
        baseCircle2.CenterSketchPoint, square2Circle.CenterSketchPoint)

    'Ground the center point of one of the circle.
    Call sketch.GeometricConstraints.AddGround(baseCircle1.CenterSketchPoint)
    
    ThisApplication.ActiveView.Fit
    
End Sub

Regards,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Message 3 of 3

Thank you, that has given me the pointers I needed, in both cases.

 

For the co-incident constraints, what I ended up doing was starting and finishing the lines from the start and end points of the arcs, rather than re-using the transient geometry points that I had created.

 

That is:

        Call oSketchBlockDef.SketchArcs.AddByCenterStartEndPoint(Coords(7), Coords(3), Coords(6), True)
        Call oSketchBlockDef.SketchArcs.AddByCenterStartEndPoint(Coords(8), Coords(4), Coords(5), False)
        Call oSketchBlockDef.SketchLines.AddByTwoPoints(oSketchBlockDef.SketchArcs(1).StartSketchPoint, oSketchBlockDef.SketchArcs(2).EndSketchPoint)
        Call oSketchBlockDef.SketchLines.AddByTwoPoints(oSketchBlockDef.SketchArcs(2).StartSketchPoint, oSketchBlockDef.SketchArcs(1).EndSketchPoint)

 

 

An example like you pasted, Philippe, would be very beneficial in the help files, which is very lacking in real-world examples for sketch-blocks.

 

=============================================
This is my signature, not part of my post.

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

Post to forums  

Autodesk Design & Make Report