Inventor Customization

Inventor Customization

Reply
Valued Contributor
nttoan8187
Posts: 92
Registered: ‎06-29-2010
Message 1 of 20 (3,921 Views)
Accepted Solution

Drawing Automation with VBA

3921 Views, 19 Replies
04-08-2011 01:17 AM

Dear,

I got a macro job for creation the drawing for a part. I got a lot of problem.

 

1/ The first one is move view:

I already create a view (e.g. oIsoView), now I want to move it to the left.

I find the DrawingView.Position method, how can I use this method? I try many time, but it's not successful yet.

 

Do I need to use the SelectSet method??????

Please mark this answer as Problem Solved if it solves your question.
-----------------------------------------------------------------------------------------
Toan
Inventor API
Employee
yuhanzhang
Posts: 448
Registered: ‎01-06-2009
Message 2 of 20 (3,894 Views)

Re: Drawing Automation with VBA

04-10-2011 10:57 PM in reply to: nttoan8187

Try below VBA code:

Sub MoveDrawingView()
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oView As DrawingView
    Set oView = oDoc.Sheets(1).DrawingViews(1)
    
    Dim oPosition As Point2d
    Set oPosition = oView.Position
    
    ' move the drawing view to left
    oPosition.X = oPosition.X - 5
    oView.Position = oPosition
    
End Sub

 



Rocky Zhang
Inventor API
Manufacturing Solutions
Autodesk, Inc.

Valued Contributor
nttoan8187
Posts: 92
Registered: ‎06-29-2010
Message 3 of 20 (3,881 Views)

Re: Drawing Automation with VBA

04-11-2011 11:49 PM in reply to: yuhanzhang

I try your code, it works well. I checked by MsgBox().

However, an problem exists before I move the view, that is:

I can not use the DrawingBreakViewAlignmentCmd command although I read and try the same as Help

        ' I create an section view , this code worked

        Dim oSectionView As SectionDrawingView
        Set oSectionView = oSheet.DrawingViews.AddSectionView(oBaseView, oSectionSketch0, oPoint11_

                                                                                  , kFromBaseDrawingViewStyle, , True, "A", False, True)
        oSectionView.ReverseDirection


        '''''''Break the Alignment of Section View
        oDrawDoc.SelectSet.Clear
        oDrawDoc.SelectSet.Select (oSectionView)

 

        Dim oCtrlDef As ControlDefinition
        Set oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("DrawingBreakViewAlignmentCmd")
       
        oCtrlDef.Execute

        oDrawDoc.SelectSet.Clear

 

I don't understand why it can not work in this case.

I create a new drawing, and this can work. What happens here???????

Thank you!

Please mark this answer as Problem Solved if it solves your question.
-----------------------------------------------------------------------------------------
Toan
Inventor API
Valued Contributor
nttoan8187
Posts: 92
Registered: ‎06-29-2010
Message 4 of 20 (3,877 Views)

Re: Drawing Automation with VBA

04-12-2011 12:36 AM in reply to: nttoan8187

I just create the same code in another modules, not my Form as upper.

It works well. Is any thing different here?

 

Sub BreakAlignment()
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oSectionView As SectionDrawingView
    Set oSectionView = oDoc.Sheets(1).DrawingViews.Item("4")
    oDoc.SelectSet.Clear
    oDoc.SelectSet.Select oSectionView
   
    Dim oCtrlDef As ControlDefinition
    Set oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("DrawingBreakViewAlignmentCmd")
   
    oCtrlDef.Execute
   
    oDoc.SelectSet.Clear
 
End Sub

 

I copy this code to my form, but it doesn't work.

I don't understand what happens?

What's different between the Module and Form in this case?

How I can run a module (eg. Module2) from my form??????

 

Many thanks in advance!

 

Please mark this answer as Problem Solved if it solves your question.
-----------------------------------------------------------------------------------------
Toan
Inventor API
Mentor
alewer
Posts: 188
Registered: ‎08-15-2006
Message 5 of 20 (3,855 Views)

Re: Drawing Automation with VBA

04-12-2011 10:26 AM in reply to: nttoan8187

The difference is that in the first case you are using parenthesis:  oDrawDoc.SelectSet.Select (oSectionView)

and none in the second case:  oDoc.SelectSet.Select oSectionView

 

In any case, it would be easier to break the alignment with:  oSectionView.Aligned = False

Valued Contributor
nttoan8187
Posts: 92
Registered: ‎06-29-2010
Message 6 of 20 (3,811 Views)

Re: Drawing Automation with VBA

04-18-2011 12:07 AM in reply to: alewer

I got that; it works very well. Thank you, Alewer!

Now is the hardest thing (with me) that is to make dimension for the drawing.

How I can select the edge as a person do? What I need to write for the program in VBA to have a drawing like this:pic2.JPG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I find in the Programing Help > Drawnig Dimensions, but I just see something very complex.

DrawingCurveSegment, DrawingCurve, GeometryIntent,Vector2d, etc.

I found something in Mr Brain blog (modthemachine), but I don't understand too

pic3.JPG

 

Is it impossible mission for me to make upper drawing by VBA?? Please help!!!!

Is there any materials that simple can be worth for me in this case???

 

Many thanks in advance!

 

 

 

 

 

Please mark this answer as Problem Solved if it solves your question.
-----------------------------------------------------------------------------------------
Toan
Inventor API
Valued Contributor
nttoan8187
Posts: 92
Registered: ‎06-29-2010
Message 7 of 20 (3,802 Views)

Re: Drawing Automation with VBA

04-18-2011 03:03 AM in reply to: nttoan8187

I got another problem with this code. I think something wrong in the Profile or BreakOutOperation.Add, but I cannot find out

                Dim oProfile0 As Profile

                Dim oPoint130 As Point2d
                Dim oBreakOutOper As BreakOutOperation               

                Set oPoint130 = oTG.CreatePoint2d(0,0)
                
                Dim oBreakOutSketch1 As DrawingSketch
                Call oTopView.Sketches.Add
                Set oBreakOutSketch1 = oTopView.Sketches.Item(1)
               
                'open the sketch for edit
                oBreakOutSketch1.Edit
                       
                'drawing a line in sketch for section line
                Dim oCircle1 As SketchCircle
                Set oCircle1 = oBreakOutSketch1.SketchCircles.AddByCenterRadius(oPoint130, 30/ 10)
               
                'exit from editing sketch
                oBreakOutSketch1.ExitEdit
               
                Set oProfile0 = oBreakOutSketch1.Profiles.AddForSolid()
                Set oBreakOutOper = oTopView.BreakOutOperations.Add(oProfile0, GeometryIntent, 100/10)

 

Many thanks in advance!

Please mark this answer as Problem Solved if it solves your question.
-----------------------------------------------------------------------------------------
Toan
Inventor API
Valued Contributor
nttoan8187
Posts: 92
Registered: ‎06-29-2010
Message 8 of 20 (3,774 Views)

Re: Drawing Automation with VBA

04-19-2011 09:44 PM in reply to: nttoan8187

I got solution by myself again........

        Dim oProfile0 As Profile
        Dim oBreakOutOper0 As BreakOutOperation

        Dim oPoint130 As Point2d

        Set oPoint130 = oTG.CreatePoint2d(50, 50)
       
        Dim oBreakOutSketch0 As DrawingSketch
        Set oBreakOutSketch0 = oBaseView.Sketches.Add
       
        'open the sketch for edit
        oBreakOutSketch0.Edit
               
        'drawing a line in sketch for section line
        Dim oCircle0 As SketchCircle
        Set oCircle0 = oBreakOutSketch0.SketchCircles.AddByCenterRadius(oPoint130, 5)
       
        'Create a collection and add the circle
        Dim oCollection0 As ObjectCollection
        Set oCollection0 = ThisApplication.TransientObjects.CreateObjectCollection
        oCollection0.Add oCircle0
       
        Set oProfile0 = oBreakOutSketch0.Profiles.AddForSolid(False, oCollection0)
       
        'exit from editing sketch
        oBreakOutSketch0.ExitEdit
       

        ' find the left horizontal line
        For Each oCurve0 In oBaseView.DrawingCurves
            If Not oCurve0.StartPoint Is Nothing And Not oCurve0.EndPoint Is Nothing Then
                If (WithinTol(oCurve0.StartPoint.x, oCurve0.EndPoint.x, 0.001) = False) Then
                    If oSelectCurve0 Is Nothing Then
                        Set oSelectCurve0 = oCurve0
                    Else
                        If oCurve0.MidPoint.x < oSelectCurve0.MidPoint.x Then
                            Set oSelectCurve0 = oCurve0
                        End If
                    End If
                End If
            End If
        Next
        

        'create GeometryIntent            
        Dim oGeometryIntentPoint0 As GeometryIntent
        Set oGeometryIntentPoint0 = osheet.CreateGeometryIntent(oSelectCurve0, kMidPointIntent)
       
        Set oBreakOutOper0 = oBaseView.BreakOutOperations.Add(oProfile0, oGeometryIntentPoint0, 0)

 

Something which is very complex is still waiting for me, that is I want to make the Centerlines for some views, using Centerline Bisector cmd, centerline cmd, and center mark. How to do this??????

 

After that, how we can choose the centerline and drawing line, I means that it seem to be the DrawingCurve is only use for Curve of Object in view, not the Annotated Line?????????

Please mark this answer as Problem Solved if it solves your question.
-----------------------------------------------------------------------------------------
Toan
Inventor API
Valued Contributor
nttoan8187
Posts: 92
Registered: ‎06-29-2010
Message 9 of 20 (3,732 Views)

Re: Drawing Automation with VBA

04-21-2011 09:05 PM in reply to: nttoan8187

I am trying to dimension my drawing as the aforementioned picture, but there are big problem here.

 I don't understand why sometimes it can get the curve and sometime it cannot?

Everything is just like a chaos here, and I cannot control this.

 

 

 

Sub baseview()
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
   
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet
   
    Dim oBaseView As DrawingView
    Set oBaseView = oSheet.DrawingViews.Item(1)
   
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry
   

'////////////////oBaseView Dimension//////////////////////
    Dim oCurve100 As DrawingCurve
    Dim oSelectCurve100, oSelectCurve101, oSelectCurve102, oSelectCurve103, oSelectCurve104 As DrawingCurve
    Set oSelectCurve100 = Nothing
    Set oSelectCurve101 = Nothing
    Set oSelectCurve102 = Nothing
    Set oSelectCurve103 = Nothing
    Set oSelectCurve104 = Nothing
   

    For Each oCurve100 In oBaseView.DrawingCurves
       If oCurve100.CurveType = kLineSegmentCurve Then
        If Not oCurve100.StartPoint Is Nothing And Not oCurve100.EndPoint Is Nothing Then
            If Round(oCurve100.StartPoint.x - oCurve100.EndPoint.x, 3) = 0 Then
               ' MsgBox (Round(oCurve100.StartPoint.y - oCurve100.EndPoint.y, 3))
'            If (WithinTol(oCurve100.StartPoint.x, oCurve100.EndPoint.x, 0.001) = True) Then
                If (Round(Math.Abs(oCurve100.StartPoint.y - oCurve100.EndPoint.y), 3) = 1.28) Then
                    Set oSelectCurve100 = oCurve100
                End If
                If (Round(Math.Abs(oCurve100.StartPoint.y - oCurve100.EndPoint.y), 3) = ((80) * 0.2 / 10)) Then
                    Set oSelectCurve101 = oCurve100
                End If
                If (Round(Math.Abs(oCurve100.StartPoint.y - oCurve100.EndPoint.y), 3) = ((132) * 0.2 / 10)) Then
                    Set oSelectCurve102 = oCurve100
                End If
                If (Round(Math.Abs(oCurve100.StartPoint.y - oCurve100.EndPoint.y), 3) = ((211.5) * 0.2 / 10)) Then
                    Set oSelectCurve103 = oCurve100
                End If
            End If
         End If
      End If
    Next

    Dim oGI100, oGI101, oGI102, oGI103, oGI104, oGI105, oGI106, oGI107, oGI108, oGI109 As GeometryIntent
    Set oGI100 = Nothing
    Set oGI101 = Nothing
    Set oGI102 = Nothing
    Set oGI103 = Nothing
    Set oGI104 = Nothing
    Set oGI105 = Nothing
    Set oGI106 = Nothing
    Set oGI107 = Nothing
    Set oGI108 = Nothing
    Set oGI109 = Nothing
   
    Set oGI100 = oSheet.CreateGeometryIntent(oSelectCurve100, kStartPointIntent)
    Set oGI101 = oSheet.CreateGeometryIntent(oSelectCurve100, kEndPointIntent)
    Set oGI102 = oSheet.CreateGeometryIntent(oSelectCurve101, kStartPointIntent)
    Set oGI103 = oSheet.CreateGeometryIntent(oSelectCurve101, kEndPointIntent)
    Set oGI104 = oSheet.CreateGeometryIntent(oSelectCurve102, kStartPointIntent)
    Set oGI105 = oSheet.CreateGeometryIntent(oSelectCurve102, kEndPointIntent)
    Set oGI106 = oSheet.CreateGeometryIntent(oSelectCurve103, kStartPointIntent)

    Dim oGenDims As GeneralDimensions
    Set oGenDims = oSheet.DrawingDimensions.GeneralDimensions
    Dim oDimPos100, oDimPos101, oDimPos102, oDimPos103, oDimPos104, oDimPos105, oDimPos106, oDimPos107, oDimPos108, oDimPos109 As Point2d
    Set oDimPos100 = Nothing
    Set oDimPos101 = Nothing
    Set oDimPos102 = Nothing
    Set oDimPos103 = Nothing
    Set oDimPos104 = Nothing
    Set oDimPos105 = Nothing
    Set oDimPos106 = Nothing
    Set oDimPos107 = Nothing
    Set oDimPos108 = Nothing
    Set oDimPos109 = Nothing
   
    If Not oSelectCurve100 Is Nothing Then
        Set oDimPos100 = oTG.CreatePoint2d(oSelectCurve100.MidPoint.x - 1, oSelectCurve100.MidPoint.y)
    End If
    If Not oSelectCurve101 Is Nothing Then
        Set oDimPos101 = oTG.CreatePoint2d(oSelectCurve101.MidPoint.x - 1.8 - 85 * 0.2 / 10, oSelectCurve101.MidPoint.y)
    End If
    If Not oSelectCurve102 Is Nothing Then
        Set oDimPos102 = oTG.CreatePoint2d(oSelectCurve102.MidPoint.x - 2.8 - (85 + (76 - 45)) * 0.2 / 10, oSelectCurve102.MidPoint.y)
    End If
    If (Not oSelectCurve100 Is Nothing) And (Not oSelectCurve101 Is Nothing) Then
        Set oDimPos103 = oTG.CreatePoint2d((oSelectCurve100.MidPoint.x + oSelectCurve101.MidPoint.x) / 2, oSelectCurve100.MidPoint.y - 211.5 * 0.2 / 2 / 10 - 1.8)
    End If
    If (Not oSelectCurve101 Is Nothing) And (Not oSelectCurve103 Is Nothing) Then
        Set oDimPos104 = oTG.CreatePoint2d((oSelectCurve101.MidPoint.x + oSelectCurve103.MidPoint.x) / 2, oSelectCurve103.StartPoint.y + 1.8)
    End If
    If (Not oSelectCurve102 Is Nothing) And (Not oSelectCurve103 Is Nothing) Then
        Set oDimPos105 = oTG.CreatePoint2d((oSelectCurve102.MidPoint.x + oSelectCurve103.MidPoint.x) / 2, oSelectCurve103.StartPoint.y + 1)
    End If
    
    Dim oLDim100, oLDim101, oLDim102, oLDim103, oLDim104, oLDim105, oLDim106, oLDim107, oLDim108, oLDim109 As LinearGeneralDimension
    'dim KK
    If Not oDimPos100 Is Nothing Then
        If (Not oGI100 Is Nothing) And (Not oGI101 Is Nothing) Then
            Set oLDim100 = oGenDims.AddLinear(oDimPos100, oGI100, oGI101, kAlignedDimensionType)
        End If
    End If
    'dim MM
    If Not oDimPos101 Is Nothing Then
        If (Not oGI102 Is Nothing) And (Not oGI103 Is Nothing) Then
            Set oLDim101 = oGenDims.AddLinear(oDimPos101, oGI102, oGI103, kAlignedDimensionType)
        End If
    End If
    'dim CD
    If Not oDimPos102 Is Nothing Then
        If (Not oGI104 Is Nothing) And (Not oGI105 Is Nothing) Then
            Set oLDim102 = oGenDims.AddLinear(oDimPos102, oGI104, oGI105, kAlignedDimensionType)
        End If
    End If
    'dim A
    If Not oDimPos103 Is Nothing Then
        If (Not oGI101 Is Nothing) And (Not oGI103 Is Nothing) Then
            Set oLDim103 = oGenDims.AddLinear(oDimPos103, oGI101, oGI103, kAlignedDimensionType)
        End If
    End If
    'dim WF
    If Not oDimPos104 Is Nothing Then
        If (Not oGI102 Is Nothing) And (Not oGI106 Is Nothing) Then
            Set oLDim104 = oGenDims.AddLinear(oDimPos104, oGI102, oGI106, kAlignedDimensionType)
        End If
    End If
    'dim VE
    If Not oDimPos105 Is Nothing Then
        If (Not oGI104 Is Nothing) And (Not oGI106 Is Nothing) Then
            Set oLDim105 = oGenDims.AddLinear(oDimPos105, oGI104, oGI106, kAlignedDimensionType)
        End If
    End If

End Sub
Private Function WithinTol(Value1 As Double, Value2 As Double, Tol As Double) As Boolean
    Math.Abs (Value1 - Value2) < Tol
End Function

 

Why I cannot get the vertical linear curve have length 1.28 cm in my drawing, I go wrong from the first curve???????????

I feel my proficiency improved a lot with the current project, more than 1700 lines of code now.

But when I do dimensions for drawing, it's is just like impossible thing??????

 

Someone who already make dimension for a difficult drawing, please answer this question: "Normal or Hard or Very Hard or Impossible??????"

 

I've waited for few days, but it seem no Autodesk guys stop by.................so sad!

 

 

 

 

 

 

 

 

 

Please mark this answer as Problem Solved if it solves your question.
-----------------------------------------------------------------------------------------
Toan
Inventor API
Valued Contributor
nttoan8187
Posts: 92
Registered: ‎06-29-2010
Message 10 of 20 (3,719 Views)

Re: Drawing Automation with VBA

04-24-2011 06:39 PM in reply to: nttoan8187

I got something currently to solve this problem.........but I wonder how to add the Ø symbol

 

    oDDim100.Text.FormattedText = "Ø" & oDDim100.Text.FormattedText

and the result is

pic4.JPG

How can I put the symbol Ø into the dimension text?

 

Please mark this answer as Problem Solved if it solves your question.
-----------------------------------------------------------------------------------------
Toan
Inventor API
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.