Add-in to create section automatically

Add-in to create section automatically

thanh.trantuan
Contributor Contributor
1,838 Views
17 Replies
Message 1 of 18

Add-in to create section automatically

thanh.trantuan
Contributor
Contributor

Hi everyone, 

I try to write an add-in to create the sections automatically in Inventor drawing after basing the model

For example, I have a model like below picture with 3 longitudinal beams and 5 transverse beams

Model.JPG

I base this model to Inventor drawing

basedDwg.JPG

and create sections like this

section.JPG

Could anyone suggest me the way to code the add-in to do this task automatically by using C#?

 

Many thanks

 

0 Likes
Accepted solutions (1)
1,839 Views
17 Replies
Replies (17)
Message 2 of 18

chandra.shekar.g
Autodesk Support
Autodesk Support

@thanh.trantuan,

 

Please provide non confidential files to test the feasibility.

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 3 of 18

thanh.trantuan
Contributor
Contributor

Dear CHANDRA,

Please see an attached model

0 Likes
Message 4 of 18

chandra.shekar.g
Autodesk Support
Autodesk Support

@thanh.trantuan,

 

Thanks for sample files to investigate,

 

To section view in drawings, it is suggested to use workplanes as shown in below image. Later, same workplanes are used for creating section line and section views are added into drawing.

 

WorkPlane_Creation.png

 

Using below VBA code, section views are added into drawing.

 

Sub Section_View()
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    Set oSheet = oDoc.ActiveSheet
    
    Dim oView As DrawingView
    Set oView = oSheet.DrawingViews.Item(1)
    
    Dim oComp As Document
    Set oComp = oView.ReferencedDocumentDescriptor.ReferencedDocument
    
    Dim wpCnt As Integer
    
    Dim i As Integer
    i = 1
    Dim h As Integer
    h = 1
    Dim v As Integer
    v = 1
    Dim wp As WorkPlane
    For wpCnt = 4 To oComp.ComponentDefinition.WorkPlanes.count
         
        Set wp = oComp.ComponentDefinition.WorkPlanes.Item(wpCnt)
        
        Call oView.SetIncludeStatus(wp, True)
        Call oView.SetVisibility(wp, False)
        
        Dim oSketch As DrawingSketch
        Set oSketch = oView.Sketches.Add
             
        Call oSketch.Edit
    
        Dim oStartPt As Point2d
        Set oStartPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).StartPoint.X, oSheet.Centerlines.Item(i).StartPoint.Y)
        
        Dim oStart As Point2d
        Set oStart = oView.SheetToDrawingViewSpace(oStartPt)
        
        Dim oEndPt As Point2d
        Set oEndPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).EndPoint.X, oSheet.Centerlines.Item(i).EndPoint.Y)
        
        Dim oEnd As Point2d
        Set oEnd = oView.SheetToDrawingViewSpace(oEndPt)
        
        Dim oLine As SketchLine
        Set oLine = oSketch.SketchLines.AddByTwoPoints(oStart, oEnd)
        
        Dim oPt As Point2d
        If oEnd.X = oStart.X Then
            Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width - (10 * v), oView.center.Y)
            Call oSketch.GeometricConstraints.AddVertical(oLine)
            v = v + 1
        Else
            Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(oView.center.X, (10 * h))
            Call oSketch.GeometricConstraints.AddHorizontal(oLine)
            h = h + 1
        End If
        
        Call oSketch.ExitEdit
        
        Call oSheet.DrawingViews.AddSectionView(oView, oSketch, oPt, kFromBaseDrawingViewStyle)
        
        i = i + 1
    Next
    
    
End Sub

To demonstrate the same, a screencast video is prepared and uploaded into below link.

 

https://autode.sk/2PI4wnq

 

The modified assembly with workplanes is attached with this post.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 5 of 18

thanh.trantuan
Contributor
Contributor

@chandra.shekar.g

 

Many thanks your reply and it's highly appreciate your great support I have some questions, could you share your thought

 

Firstly, in order to use your code, we have to create new planes which is constraint with existing structures. Could we have another way to do without create new planes like this. As you see, in the skeleton.ipt, these workplanes are created already, so can we use them to position the sketch lines

 

Secondly, can we constraint these sketches after creating them

 

Thirdly, in the section properties, the section depth is full, could it be set up with the distance (such as 50 mm or 100mm) to show the section at this area only.

 

Thanks & Best Regards

0 Likes
Message 6 of 18

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

is it possible to get longer section lines (text should be outside of the geometry)?

 

Thank you Georg

Message 7 of 18

chandra.shekar.g
Autodesk Support
Autodesk Support

@GeorgK,

 

At below code, extension of lines can be added depends on line type (Like Horizontal or Vertical).

Dim oStartPt As Point2d
        Set oStartPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).StartPoint.X, oSheet.Centerlines.Item(i).StartPoint.Y)
        
        Dim oStart As Point2d
        Set oStart = oView.SheetToDrawingViewSpace(oStartPt)
        
        Dim oEndPt As Point2d
        Set oEndPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).EndPoint.X, oSheet.Centerlines.Item(i).EndPoint.Y)
        
        Dim oEnd As Point2d
        Set oEnd = oView.SheetToDrawingViewSpace(oEndPt)
        
        Dim oLine As SketchLine
        Set oLine = oSketch.SketchLines.AddByTwoPoints(oStart, oEnd)

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 8 of 18

chandra.shekar.g
Autodesk Support
Autodesk Support

@thanh.trantuan,

 

Firstly, in order to use your code, we have to create new planes which is constraint with existing structures. Could we have another way to do without create new planes like this. As you see, in the skeleton.ipt, these workplanes are created already, so can we use them to position the sketch lines - The same code is modified to get planes from skeleton.ipt

Sub Section_View()
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    Set oSheet = oDoc.ActiveSheet
    
    Dim oView As DrawingView
    Set oView = oSheet.DrawingViews.Item(1)
    
    Dim occ As ComponentOccurrence
    If oView.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
        Dim oComp As AssemblyDocument
        Set oComp = oView.ReferencedDocumentDescriptor.ReferencedDocument
        
        Set occ = oComp.ComponentDefinition.Occurrences.ItemByName("Skeleton:1")
        
        Dim oSkeletonDoc As PartDocument
        Set oSkeletonDoc = occ.Definition.Document
    Else
        Exit Sub
    End If
    
    Dim wpCnt As Integer
    
    Dim i As Integer
    i = 1
    Dim h As Integer
    h = 1
    Dim v As Integer
    v = 1
    Dim wp As WorkPlane
    For Each wp In oSkeletonDoc.ComponentDefinition.WorkPlanes
         
        If wp.Name Like "T*" Or wp.Name Like "L*" Then
            Dim oProxy As WorkPlaneProxy
            Call occ.CreateGeometryProxy(wp, oProxy)
            Call oView.SetIncludeStatus(oProxy, True)
            Call oView.SetVisibility(oProxy, False)
            
            Dim oSketch As DrawingSketch
            Set oSketch = oView.Sketches.Add
                 
            Call oSketch.Edit
        
            Dim oStartPt As Point2d
            Set oStartPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).StartPoint.X, oSheet.Centerlines.Item(i).StartPoint.Y)
            
            Dim oStart As Point2d
            Set oStart = oView.SheetToDrawingViewSpace(oStartPt)
            
            Dim oEndPt As Point2d
            Set oEndPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).EndPoint.X, oSheet.Centerlines.Item(i).EndPoint.Y)
            
            Dim oEnd As Point2d
            Set oEnd = oView.SheetToDrawingViewSpace(oEndPt)
            
            Dim oLine As SketchLine
            Set oLine = oSketch.SketchLines.AddByTwoPoints(oStart, oEnd)
            
            Dim oPt As Point2d
            If oEnd.X = oStart.X Then
                Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width - (10 * v), oView.center.Y)
                Call oSketch.GeometricConstraints.AddVertical(oLine)
                v = v + 1
            Else
                Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(oView.center.X, (10 * h))
                Call oSketch.GeometricConstraints.AddHorizontal(oLine)
                h = h + 1
            End If
            
            Call oSketch.ExitEdit
            
            Call oSheet.DrawingViews.AddSectionView(oView, oSketch, oPt, kFromBaseDrawingViewStyle)
            
            i = i + 1
        End If
    Next
    
    
End Sub

Secondly, can we constraint these sketches after creating them - Yes, it constrained to vertical or horizontal.

 

Thirdly, in the section properties, the section depth is full, could it be set up with the distance (such as 50 mm or 100mm) to show the section at this area only. - Hoping the same answer to Geroge will help this.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 9 of 18

sam
Advocate
Advocate

Hi @chandra.shekar.g

 

thanks for this. Would you mind adding bit of comments to each part describing what the code does. 

 

Best regards, 

Sam

@chandra.shekar.g wrote:

@thanh.trantuan,

 

Firstly, in order to use your code, we have to create new planes which is constraint with existing structures. Could we have another way to do without create new planes like this. As you see, in the skeleton.ipt, these workplanes are created already, so can we use them to position the sketch lines - The same code is modified to get planes from skeleton.ipt

 

Sub Section_View()
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    Set oSheet = oDoc.ActiveSheet
    
    Dim oView As DrawingView
    Set oView = oSheet.DrawingViews.Item(1)
    
    Dim occ As ComponentOccurrence
    If oView.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
        Dim oComp As AssemblyDocument
        Set oComp = oView.ReferencedDocumentDescriptor.ReferencedDocument
        
        Set occ = oComp.ComponentDefinition.Occurrences.ItemByName("Skeleton:1")
        
        Dim oSkeletonDoc As PartDocument
        Set oSkeletonDoc = occ.Definition.Document
    Else
        Exit Sub
    End If
    
    Dim wpCnt As Integer
    
    Dim i As Integer
    i = 1
    Dim h As Integer
    h = 1
    Dim v As Integer
    v = 1
    Dim wp As WorkPlane
    For Each wp In oSkeletonDoc.ComponentDefinition.WorkPlanes
         
        If wp.Name Like "T*" Or wp.Name Like "L*" Then
            Dim oProxy As WorkPlaneProxy
            Call occ.CreateGeometryProxy(wp, oProxy)
            Call oView.SetIncludeStatus(oProxy, True)
            Call oView.SetVisibility(oProxy, False)
            
            Dim oSketch As DrawingSketch
            Set oSketch = oView.Sketches.Add
                 
            Call oSketch.Edit
        
            Dim oStartPt As Point2d
            Set oStartPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).StartPoint.X, oSheet.Centerlines.Item(i).StartPoint.Y)
            
            Dim oStart As Point2d
            Set oStart = oView.SheetToDrawingViewSpace(oStartPt)
            
            Dim oEndPt As Point2d
            Set oEndPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).EndPoint.X, oSheet.Centerlines.Item(i).EndPoint.Y)
            
            Dim oEnd As Point2d
            Set oEnd = oView.SheetToDrawingViewSpace(oEndPt)
            
            Dim oLine As SketchLine
            Set oLine = oSketch.SketchLines.AddByTwoPoints(oStart, oEnd)
            
            Dim oPt As Point2d
            If oEnd.X = oStart.X Then
                Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width - (10 * v), oView.center.Y)
                Call oSketch.GeometricConstraints.AddVertical(oLine)
                v = v + 1
            Else
                Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(oView.center.X, (10 * h))
                Call oSketch.GeometricConstraints.AddHorizontal(oLine)
                h = h + 1
            End If
            
            Call oSketch.ExitEdit
            
            Call oSheet.DrawingViews.AddSectionView(oView, oSketch, oPt, kFromBaseDrawingViewStyle)
            
            i = i + 1
        End If
    Next
    
    
End Sub

 

Secondly, can we constraint these sketches after creating them - Yes, it constrained to vertical or horizontal.

 

Thirdly, in the section properties, the section depth is full, could it be set up with the distance (such as 50 mm or 100mm) to show the section at this area only. - Hoping the same answer to Geroge will help this.

 

Thanks and regards,


 

0 Likes
Message 10 of 18

thanh.trantuan
Contributor
Contributor

@chandra.shekar.g 

Many thanks for your great support

Regarding to 3rd question, I mean the depth value when we edit section properties. Section depth is "distance" instead of "Full"

1.jpg

Besides, I try to arrange the section, break the alignment and rotate the vertical section 

3.JPG

I see that in your code, the distance between each section can be controlled, so how about rotate these vertical sections?

 

Many thanks

0 Likes
Message 11 of 18

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@thanh.trantuan,

 

Try below code to change depth of section and rotate the section view.

 

Dim oSection As SectionDrawingView
            Set oSection = oSheet.DrawingViews.AddSectionView(oView, oSketch, oPt, kFromBaseDrawingViewStyle)
            
            oSection.SectionDepth = 127
            
            Call oSection.RotateByAngle(1.57, True)

 

Full modified code is as below.

 

Sub Section_View()
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    Set oSheet = oDoc.ActiveSheet
    
    Dim oView As DrawingView
    Set oView = oSheet.DrawingViews.Item(1)
    
    Dim occ As ComponentOccurrence
    If oView.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
        Dim oComp As AssemblyDocument
        Set oComp = oView.ReferencedDocumentDescriptor.ReferencedDocument
        
        Set occ = oComp.ComponentDefinition.Occurrences.ItemByName("Skeleton:1")
        
        Dim oSkeletonDoc As PartDocument
        Set oSkeletonDoc = occ.Definition.Document
    Else
        Exit Sub
    End If
    
    Dim wpCnt As Integer
    
    Dim i As Integer
    i = 1
    Dim h As Integer
    h = 1
    Dim v As Integer
    v = 1
    Dim wp As WorkPlane
    For Each wp In oSkeletonDoc.ComponentDefinition.WorkPlanes
         
        If wp.Name Like "T*" Or wp.Name Like "L*" Then
            Dim oProxy As WorkPlaneProxy
            Call occ.CreateGeometryProxy(wp, oProxy)
            Call oView.SetIncludeStatus(oProxy, True)
            Call oView.SetVisibility(oProxy, False)
            
            Dim oSketch As DrawingSketch
            Set oSketch = oView.Sketches.Add
                 
            Call oSketch.Edit
        
            Dim oStartPt As Point2d
            Set oStartPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).StartPoint.X, oSheet.Centerlines.Item(i).StartPoint.Y)
            
            Dim oStart As Point2d
            Set oStart = oView.SheetToDrawingViewSpace(oStartPt)
            
            Dim oEndPt As Point2d
            Set oEndPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).EndPoint.X, oSheet.Centerlines.Item(i).EndPoint.Y)
            
            Dim oEnd As Point2d
            Set oEnd = oView.SheetToDrawingViewSpace(oEndPt)
            
            Dim oLine As SketchLine
            Set oLine = oSketch.SketchLines.AddByTwoPoints(oStart, oEnd)
            
            Dim oPt As Point2d
            If oEnd.X = oStart.X Then
                Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width - (10 * v), oView.center.Y)
                Call oSketch.GeometricConstraints.AddVertical(oLine)
                v = v + 1
            Else
                Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(oView.center.X, (10 * h))
                Call oSketch.GeometricConstraints.AddHorizontal(oLine)
                h = h + 1
            End If
            
            Call oSketch.ExitEdit
            
            Dim oSection As SectionDrawingView
            Set oSection = oSheet.DrawingViews.AddSectionView(oView, oSketch, oPt, kFromBaseDrawingViewStyle)
            
            oSection.SectionDepth = 127
            
            Call oSection.RotateByAngle(1.57, True)
            
            i = i + 1
        End If
    Next
    
    
End Sub

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 12 of 18

thanh.trantuan
Contributor
Contributor

@chandra.shekar.g 

Many thanks for your support, it's all clear now

0 Likes
Message 13 of 18

thanh.trantuan
Contributor
Contributor

@chandra.shekar.g 

Sorry, I just have one question that 

If I use frame generator to create stiffener, so it means that there are no work planes at the position of stiffener (because frame generator generate beams from sketch line). I want to make sections at the position of stiffeners by picking the part's edge (step 1) but it cannot work. Can you suggest me to do this task like this?1.JPG

Many thanks

0 Likes
Message 14 of 18

thanh.trantuan
Contributor
Contributor

@chandra.shekar.g 

Could you please take a look this question and give me any supports

 

Sorry, I just have one question that 

If I use frame generator to create stiffener, so it means that there are no work planes at the position of stiffener (because frame generator generate beams from sketch line). I want to make sections at the position of stiffeners by picking the part's edge (step 1) but it cannot work. Can you suggest me to do this task like this?

1.JPG

 

 

 

0 Likes
Message 15 of 18

chandra.shekar.g
Autodesk Support
Autodesk Support

@thanh.trantuan,

 

Are you trying through UI?

 

Please provide non confidential drawing files and supporting files to test the feasibility.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 16 of 18

thanh.trantuan
Contributor
Contributor

@chandra.shekar.g 

I attached model here, please take a look 

 

Besides, if it's possible, may I ask for help about customizing the text format of the section view like picture below by using VBA

11.JPG

there are 2 options, firstly is change single text of the section view, secondly is change all text of the all section view within the drawing sheet.

 

 

Many thanks

0 Likes
Message 17 of 18

chandra.shekar.g
Autodesk Support
Autodesk Support

@thanh.trantuan,

 

Try below code to change the label of drawing view.

 

Dim oSection As SectionDrawingView
            Set oSection = oSheet.DrawingViews.AddSectionView(oView, oSketch, oPt, kFromBaseDrawingViewStyle)
            
            oSection.Label.FormattedText = "<StyleOverride Bold='True' Underline='True' FontSize='0.8'>SECTION <DrawingViewName/>-<DrawingViewName/></StyleOverride><Br/>SCALE <DrawingViewScale/>"
            

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 18 of 18

thanh.trantuan
Contributor
Contributor

@chandra.shekar.g 

 

Thank you so much, so how about the question 

"Sorry, I just have one question that 

If I use frame generator to create stiffener, so it means that there are no work planes at the position of stiffener (because frame generator generate beams from sketch line). I want to make sections at the position of stiffeners by picking the part's edge (step 1) but it cannot work. Can you suggest me to do this task like this?"1.JPG

could you give me any clues about this?

Thanks in advance

0 Likes