VBA Make a Hexagone

VBA Make a Hexagone

Anonymous
Not applicable
747 Views
3 Replies
Message 1 of 4

VBA Make a Hexagone

Anonymous
Not applicable

Hi, and thanks for reading!

 

Private Function buildPoligone(oparamD) As SketchBlock


    
    Dim SelectedPoint As Inventor.SketchPoint
    Set SelectedPoint = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchPointFilter, "Select the point...")
    
    ThisApplication.CommandManager.StopActiveCommand
    
    ' Connect to a running instance of Inventor.
    Dim invApp As Inventor.Application
    Set invApp = ThisApplication
    'System.Runtime.InteropServices.Marshal.GetActiveObject
                                                '("Inventor.Application")
    ' Set a reference to the active sketch.
    Dim oSketch As PlanarSketch
    Set oSketch = ThisApplication.ActiveEditObject

    ' Get a reference to the TransientGeometry object.
    Dim tg As Inventor.TransientGeometry
    Set tg = invApp.TransientGeometry
    
    
    ' Get a reference to the SketchLines collection.
    Dim olines As Inventor.SketchLines
    Set olines = oSketch.SketchLines
    
    ' Get a reference to the SketchCircle collection.
    Dim oCircles As Inventor.SketchCircles
    Set oCircles = oSketch.SketchCircles
    
    

    Dim oCircle1 As Inventor.SketchCircle
    Set oCircle1 = oCircles.AddByCenterRadius(SelectedPoint, oparamD)
    oparamRi = oparamRi / 2
    oparamRc = oparamRi / (Math.Sqr(3) / 2)
    
    Dim oInscribedPoint As Inventor.SketchPoint
    Set oInscribedPoint = SelectedPoint
    
    oInscribedPoint.Geometry.X = SelectedPoint.Geometry.X - oparamRc
    oInscribedPoint.Geometry.Y = SelectedPoint.Geometry.Y
    
' Define inputs needed to rotate.
    Dim col As ObjectCollection
    Set col = ThisApplication.TransientObjects.CreateObjectCollection
    
    Call col.Add(oInscribedPoint)
    
    Dim i As Long
    Dim rotatePoint As Point2d
    Set rotatePoint = tg.CreatePoint2d(0, 0)
    rotatePoint.X = SelectedPoint.Geometry.X
    rotatePoint.Y = SelectedPoint.Geometry.Y
    Dim pi As Double
    pi = Atn(1) * 4

    Dim Angle As Double
    Angle = Me.Angle.Value * (pi / 180)

    For i = 1 To 6
    
    Dim colsub As ObjectCollection
    Set colsub = ThisApplication.TransientObjects.CreateObjectCollection
        
    colsub.Add (oSketch.RotateSketchObjects(col, rotatePoint, Angle + 60 * (i)))
    
    Next
    Dim oline As Inventor.SketchLine
    

    Dim StPoint As Object
    Set StPoint = colsub(1)
    Dim EnPoint As Object
    Set EnPoint = colsub(2)
    Set oline = olines.AddByTwoPoints(StPoint, EnPoint)
      
End Function

After so much pain with this problem, I still can t make a simple hexagone with VBA. Here the object collection (named "subcol") is not filling up (shun't I get 6 object in it?) . 

Why does this synthaxe:

colsub.Item(i).Add (oSketch.RotateSketchObjects(col, rotatePoint, Angle + 60 * (i)))

..that seems to be used all over exemples, doesn t work in my case?

 

Also tryed the methode to make a hexagone directly without any succes...

 Dim oline1 As Inventor.SketchLine
    
    Set oline1 = olines.AddByTwoPoints(oInscribedPoint.Geometry.X, SelectedPoint
    
    Dim opoly As Inventor.Polyline2d
    Dim oSides As Long
    oSides = 6
    
    opoly = olines.AddAsPolygon(6, SelectedPoint, oInscribedPoint, True)

Any clues?

0 Likes
748 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

Ok, just simply how can I make a hexagone with VBA? I just can t make any working code for it. Feels awful!

I have aready all the coordonate setted up in formulas, but unable to do anything with it.

0 Likes
Message 3 of 4

bradeneuropeArthur
Mentor
Mentor
Why don't you use the inventor command for this!?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 4 of 4

BrianEkins
Mentor
Mentor

Here's a little VBA example that uses the AddAsPolygon method to create a hexagon.

Public Sub CreateHexagon()
    Dim sk As sketch
    Set sk = ThisApplication.ActiveEditObject
    
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry
    Dim results As SketchEntitiesEnumerator
    Set results = sk.SketchLines.AddAsPolygon(6, tg.CreatePoint2d(0, 0), tg.CreatePoint2d(6, 0), True)
End Sub
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes