VBA Rotate Collection Object

VBA Rotate Collection Object

Anonymous
Not applicable
1,419 Views
4 Replies
Message 1 of 5

VBA Rotate Collection Object

Anonymous
Not applicable

 Hi fellows!

Can I get any advise for setting up my collection? Here the rotate method wont work ... "mismatch'

 ' Create a new object collection
Dim oCollection As ObjectCollection
Set oCollection = invApp.TransientObjects.CreateObjectCollection


Call oCollection.Add(oline1)
Call oCollection.Add(oline2)
Call oCollection.Add(oline3)
Call oCollection.Add(oline4)
Call oCollection.Add(oline5)
Call oCollection.Add(oline6)

'attempt to rotate
Call oSketch.RotateSketchObjects(oCollection, oSelect, oAngle)

 

0 Likes
Accepted solutions (1)
1,420 Views
4 Replies
Replies (4)
Message 2 of 5

BrianEkins
Mentor
Mentor

I suspect there's a problem with your rotate point or angle arguments.  Here's a full macro that illustrates all of the required input.

 

Public Sub RotateTest()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    
    ' Get the first sketch in the part.
    Dim sketch As PlanarSketch
    Set sketch = partDoc.ComponentDefinition.Sketches.Item(1)
       
    ' Create a new object collection
    Dim col As ObjectCollection
    Set col = ThisApplication.TransientObjects.CreateObjectCollection
    
    ' Add all of the lines in the sketch to the collection.
    Dim line As SketchLine
    For Each line In sketch.SketchLines
        Call col.Add(line)
    Next
     
    ' Define inputs needed to rotate.
    Dim rotatePoint As Point2d
    Set rotatePoint = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
    Dim pi As Double
    pi = Atn(1) * 4
    Dim angle As Double
    angle = 45 * (pi / 180)
        
    'attempt to rotate
    Call sketch.RotateSketchObjects(col, rotatePoint, angle)
End Sub
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5

BrianEkins
Mentor
Mentor
Accepted solution

Rotating lines has some weird behavior that's either a bug or something I don't understand.  Because sketch geometry is all dependent on sketch points I've used that create a better version that does give the results I expect.

 

Public Sub RotateTest2()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    
    ' Get the first sketch in the part.
    Dim sketch As PlanarSketch
    Set sketch = partDoc.ComponentDefinition.Sketches.Item(1)
       
    ' Create a new object collection
    Dim col As ObjectCollection
    Set col = ThisApplication.TransientObjects.CreateObjectCollection
    
    ' Add all of the lines in the sketch to the collection.
    Dim line As SketchLine
    For Each line In sketch.SketchLines
        Call col.Add(line.StartSketchPoint)
        Call col.Add(line.EndSketchPoint)
    Next
     
    ' Define inputs needed to rotate.
    Dim rotatePoint As Point2d
    Set rotatePoint = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
    Dim pi As Double
    pi = Atn(1) * 4
    Dim angle As Double
    angle = 45 * (pi / 180)
        
    'attempt to rotate
    Call sketch.RotateSketchObjects(col, rotatePoint, angle)
End Sub
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 4 of 5

Anonymous
Not applicable

Yes you where right. my declaration of the center point was a SketchPoint but needed a Point2d.

    'Selected point
    Dim Nokta As Inventor.SketchPoint
    Set Nokta = invApp.CommandManager.Pick(SelectionFilterEnum.kSketchPointFilter, "Select the point...")
    
    Dim oRotatePoint As Point2d
    Set oRotatePoint = ThisApplication.TransientGeometry.CreatePoint2d(Nokta.Geometry.X, Nokta.Geometry.Y)

So using you r declaration and enter the coordinate of the point, it now work fine. It seems like I don t have any problems otherwise rotating lines...

0 Likes
Message 5 of 5

Anonymous
Not applicable

I also saw you r other posts on rotating objects, and couldn t make work any of the formulas to convert to Radian..... No clue why, but any way you give Atn(1) as a product  or Pi, it will return you a constant value for oRad. Even with different oAngle. So with a little patience and brut-forcing. I found that the simplest way to convert the angle, was the way to make it work.

 

'get Radian
    Dim oRad As Double
    oRad = (oAngle / 180) * 3.14159265358979 'Using constant Pi doesn t work neither

Yes that s ugly but who cares? It finely work x)

 

0 Likes