Drawing sketch line linked to work plane

Drawing sketch line linked to work plane

malmal02122023
Advocate Advocate
1,483 Views
31 Replies
Message 1 of 32

Drawing sketch line linked to work plane

malmal02122023
Advocate
Advocate

Hello,

 

Could you help me please?

I am new in VBA ilogic

I would like to link a sketch line to work plane with offset 20 mm and use it to create section line.

I understand that the code shown below draws a line on the base view relative to its center.

Question: how can  build a line relative to the work plane located in the model and named A-A.

 

Thanks for advice

Dim SectionPoint_L As Point2d = oTG.CreatePoint2d(Left_X, oBaseView.Center.Y)
	Dim SectionPoint_R As Point2d = oTG.CreatePoint2d(Right_X, oBaseView.Center.Y)
	Dim SheetPoint_L As Point2d = SectionSketch.SheetToSketchSpace(SectionPoint_L)
	Dim SheetPoint_R As Point2d = SectionSketch.SheetToSketchSpace(SectionPoint_R)
	Dim SketchLine As Inventor.SketchLine = SectionSketch.SketchLines.AddByTwoPoints(SheetPoint_L, SheetPoint_R)

malmal02122023_0-1705176446376.png

 

0 Likes
Accepted solutions (1)
1,484 Views
31 Replies
Replies (31)
Message 2 of 32

A.Acheson
Mentor
Mentor

Hi @malmal02122023 

Your request does involve knowing the Inventor API in both assembly and drawing very well.

 

Linked in a previous post here is the below code. This will not work in your model but is a starting sample to learn what you need. Create a simple assembly with one occurrence. Place into the drawing this assembly. Manually check the workplane required can be made visible. If it can then run the code. 

Sub Main
     SetWorkPlaneVisibilityFromAsm()
End Sub

Public Sub SetWorkPlaneVisibilityFromAsm()

    Dim oDrawingDoc As DrawingDocument

    oDrawingDoc = ThisApplication.ActiveDocument

    'Get referenced Assembly document

    Dim oAsm As AssemblyDocument

    oAsm = oDrawingDoc.ReferencedDocuments(1)

    'Get first occurrence, suppose it's a Part

    Dim oOcc As ComponentOccurrence

    oOcc = oAsm.ComponentDefinition.Occurrences(1)

    Dim oPartCompDef As PartComponentDefinition

    oPartCompDef = oOcc.Definition

    'Get YZ WorkPlane, suppose it's perpendicular to the view

    Dim oWP As WorkPlane

    oWP = oPartCompDef.WorkPlanes.Item("YZ Plane")

    'Create proxy object

    Dim oWPpx As WorkPlaneProxy

    Call oOcc.CreateGeometryProxy(oWP, oWPpx)

    'Set visibility

    Call oDrawingDoc.Sheets(1).DrawingViews(1).

                        SetVisibility(oWPpx, True)

End Sub

In order to dig down into the occurrence thst holds the workplane you will need to target the leaf occurrences of the assembly, this article will show you how

The below code will show you how to access leaf occurrences in your assembly. Open your assembly and run the code. 

Sub Main
    GetPartOccurrences()
End Sub

Public Sub GetPartOccurrences()
    ' Get the active assembly.
    Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

    ' Get the assembly component definition.
    Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

    ' Get all of the leaf occurrences of the assembly.
    Dim oLeafOccs As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences

    ' Iterate through the occurrences and print the name.
    Dim oOcc As ComponentOccurrence
    For Each oOcc In oLeafOccs
        Logger.Info(oOcc.Name)
    Next
End Sub

 

Now integrate the two codes together. So you are not just looking at occurrence 1 but rather all occurrences. You will need further filtering to pick up the workplane by name. Hopefully this gets you started. 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 32

malmal02122023
Advocate
Advocate

Hello,

 

Ok, it is done. 
Could you answer the question,  how to filter the name of work planes?

 

Sub Main()
    Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    oSheet = oDoc.ActiveSheet
    
    Dim oFirst_view As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Please select the main view")
    If oFirst_view Is Nothing Then Exit Sub
    Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
    
    Dim occ As ComponentOccurrence
    If oFirst_view.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
    Dim oComp As AssemblyDocument
    oComp = oFirst_view.ReferencedDocumentDescriptor.ReferencedDocument
        
    occ = oComp.ComponentDefinition.Occurrences.ItemByName("Skeleton test:1")
        
    Else
        Exit Sub
    End If
    
    	
    Dim wp As WorkPlane
	
	Dim oSkeletonDoc As PartDocument
	oSkeletonDoc = occ.Definition.Document
	
    For Each wp In oSkeletonDoc.ComponentDefinition.WorkPlanes
		
	         
        If wp.Name Like "1" Then
            Dim oProxy As WorkPlaneProxy
            Call occ.CreateGeometryProxy(wp, oProxy)
            Call oFirst_view.SetIncludeStatus(oProxy, True)
            Call oFirst_view.SetVisibility(oProxy, True)
		End If
            
    Next
    
    
End Sub

 

Best regards

0 Likes
Message 4 of 32

A.Acheson
Mentor
Mentor

It looks good, I didn't test it but it looks like everything is in the right place. 

 

You can either directly find the name or do a wild card.

If wp.Name = "1" Then
End If

Or

If wp.Name.Contains("1") Then
End If

 
End If

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 32

malmal02122023
Advocate
Advocate

It’s clear that I can manually rewrite the value in the code. But I would like to call up a dialog box with work planes and select from it for their further creation.

0 Likes
Message 6 of 32

A.Acheson
Mentor
Mentor

In that case add the names to a listbox and present the user the list. Take the proxy creation outside the loop. 

 

See help page here for list box

Dim materialList as New List (Of String)
materialList.add("steel")
material = InputListBox("Choose Part material", materialList, _ material, Title := "Part material", ListName := "Available Standard materials")

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 32

malmal02122023
Advocate
Advocate

The code was changed as you describe

 

But I have error here. Could You help me please with this?

Dim wp.Name As String = InputListBox("Work planes found", wpList, "", "Select work plane", "work plane found")

 

Sub Main()
    Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument
    
	    Dim oSheet As Sheet
    oSheet = oDoc.ActiveSheet
    
    Dim oFirst_view As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Please select the main view")
    If oFirst_view Is Nothing Then Exit Sub
    Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
    
    Dim occ As ComponentOccurrence
    If oFirst_view.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
    Dim oComp As AssemblyDocument
    oComp = oFirst_view.ReferencedDocumentDescriptor.ReferencedDocument
        
    occ = oComp.ComponentDefinition.Occurrences.ItemByName("Skeleton test:1")
        
    Else
        Exit Sub
    End If
    
    	
    Dim wp As WorkPlane
	
	Dim oSkeletonDoc As PartDocument
	oSkeletonDoc = occ.Definition.Document
	
    For Each wp In oSkeletonDoc.ComponentDefinition.WorkPlanes
		
        
	
		Dim wpList As New List(Of String)
		If wpList.Count = 0 Then Return
wpList.Add("work plane")
Dim wp.Name As String = InputListBox("Work planes found", wpList, "", "Select work plane", "work plane found")
If String.IsNullOrEmpty(wp.Name) Then Return
	
	
		do
									
		''If wp.Name.Contains("AA") Then
            Dim oProxy As WorkPlaneProxy
            Call occ.CreateGeometryProxy(wp, oProxy)
            Call oFirst_view.SetIncludeStatus(oProxy, True)
            Call oFirst_view.SetVisibility(oProxy, True)
		''End If
         Loop   
    Next
    
  
End Sub

 

 

0 Likes
Message 8 of 32

A.Acheson
Mentor
Mentor

It looks like your trying to set/change the name which would not be your Intention.

Also you are no longer able to get  the workplane names and create its proxy in the lloop. Instead get the workplane object by its name outside the for loop and then proxy creation.

Dim wpList As New List(Of String)

For
Each wp In oSkeletonDoc.ComponentDefinition.WorkPlanes wpList.Add("wp.Name")
Next Dim wpSel As String = InputListBox("Work planes found", wpList, "", "Select work plane", "work plane found") If String.IsNullOrEmpty(wpSel) Then Return wp = oSkeletonDoc.ComponentDefinition.WorkPlane(wpSel)
Dim oProxy As WorkPlaneProxy
occ.CreateGeometryProxy(wp, oProxy)
oFirst_view.SetIncludeStatus(oProxy, True)
First_view.SetVisibility(oProxy, True)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 9 of 32

malmal02122023
Advocate
Advocate

That is, I need to change

 

wpList.Add("wp.Name") to wpList.Add(wp.Name)

for the list to appear work planes name

 

But appear this error

No public WorkPlane member found for type PartComponentDefinition.

0 Likes
Message 10 of 32

A.Acheson
Mentor
Mentor

Apologies a few spelling errors, you caught the first and the second is that the object wp should be retrieved from the workplanes  collection. So add an s to workplane. The error message is correct. 

Corrected line. 

oSkeletonDoc.ComponentDefinition.WorkPlanes(wpSel)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 11 of 32

malmal02122023
Advocate
Advocate

Hello,


It works.

But the ultimate goal is to create a section along the work plane. What are my next steps?

Thank you

Best regards

0 Likes
Message 12 of 32

A.Acheson
Mentor
Mentor

The next step is to get the drawing curve you just made visible/ included by using drawingcurvesenumerator. 

Syntax

SectionDrawingView.DrawingCurves( [ModelObject] As Variant ) As DrawingCurvesEnumerator

 

 

And because you have only one Enumerator in your curve you can simply set it as item 1 to get the drawing curve.

Syntax

DrawingCurvesEnumerator.ItemIndex As Long ) As DrawingCurve

 

Once you have a drawing Curve you can get its start and end point as point 2d which you can then input into a view sketch and create a sketch line see API sample here written in vba. Convert as required.

Syntax

DrawingCurve.StartPoint() As Point2d

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 13 of 32

malmal02122023
Advocate
Advocate

Something like this?

 

Sub Main()
    Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument
    
	    Dim oSheet As Sheet
    oSheet = oDoc.ActiveSheet
    
    Dim oFirst_view As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Please select the main view")
    If oFirst_view Is Nothing Then Exit Sub
    Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
    
    Dim occ As ComponentOccurrence
    If oFirst_view.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
    Dim oComp As AssemblyDocument
    oComp = oFirst_view.ReferencedDocumentDescriptor.ReferencedDocument
        
    occ = oComp.ComponentDefinition.Occurrences.ItemByName("Skeleton test:1")
        
    Else
        Exit Sub
    End If
    
    	
    Dim wp As WorkPlane
	
	Dim oSkeletonDoc As PartDocument
	oSkeletonDoc = occ.Definition.Document
	
     
			
		Dim wpList As New List(Of String)

For Each wp In oSkeletonDoc.ComponentDefinition.WorkPlanes
      wpList.Add(wp.Name)
Next
Dim wpSel As String = InputListBox("Work planes found", wpList, "", "Select work plane", "work plane found")
If String.IsNullOrEmpty(wpSel) Then Return
wp = oSkeletonDoc.ComponentDefinition.WorkPlanes(wpSel)

Dim oProxy As WorkPlaneProxy 
occ.CreateGeometryProxy(wp, oProxy) 
oFirst_view.SetIncludeStatus(oProxy, True) 
oFirst_view.SetVisibility(oProxy, True)



 Dim oSketch As DrawingSketch
            oSketch = oFirst_view.Sketches.Add
                 
            Call oSketch.Edit
        
            Dim oStartPt As Point2d
            oStartPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).StartPoint.X, oSheet.Centerlines.Item(i).StartPoint.Y)
            
            Dim oStart As Point2d
            oStart = oFirst_view.SheetToDrawingViewSpace(oStartPt)
            
            Dim oEndPt As Point2d
            oEndPt = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Centerlines.Item(i).Name.EndPoint.X, oSheet.Centerlines.Item(i).EndPoint.Y)
            
            Dim oEnd As Point2d
            oEnd = oFirst_view.SheetToDrawingViewSpace(oEndPt)
            
            Dim oLine As SketchLine
            oLine = oSketch.SketchLines.AddByTwoPoints(oStart, oEnd)
 
            Call oSketch.ExitEdit

    
  
End Sub

 

0 Likes
Message 14 of 32

A.Acheson
Mentor
Mentor

I didn't dig into this much but first off typing in option explicit on to see how variables are bring up these errors. 

AAcheson_0-1705352275767.png

 

Here is a quick  method to place a sketch line over the existing included workplane.

	Dim curve1 As DrawingCurve = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCurveSegmentFilter, "Select a drawing Curve").Parent
	Dim centerline As Centerline  = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCenterlineFilter, "Select a centerline")
	Dim drawView As DrawingView = curve1.Parent

	Dim sketch As DrawingSketch = drawView.Sketches.Add
	
	sketch.Edit()

	Dim line1 As SketchLine = sketch.AddByProjectingEntity(centerline)

	Dim line2 As SketchLine = sketch.SketchLines.AddByTwoPoints(line1.Geometry.StartPoint,line1.Geometry.EndPoint)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 15 of 32

malmal02122023
Advocate
Advocate

Thank you for help

 

Points 2d added and created line.

What is next?

 

Dim oStartPoint As Point2d
	
	oStartPoint = ThisApplication.TransientGeometry.CreatePoint2d(line2.Geometry.StartPoint.X, line2.Geometry.StartPoint.Y+1)
        
    Dim oEndPoint As Point2d
	
	oEndPoint = ThisApplication.TransientGeometry.CreatePoint2d(line2.Geometry.EndPoint.X, line2.Geometry.EndPoint.Y+1)
	
	Dim oLine As SketchLine
            oLine = sketch.SketchLines.AddByTwoPoints(oStartPoint, oEndPoint)

 

0 Likes
Message 16 of 32

A.Acheson
Mentor
Mentor

Well I guess dimensioning/ constraining your line is next. You should now have a solid sketchline overlapping the center line. How do you want the drawing to look? You could use the workplane directly to section or put in a dimension offset to use the sketchline. If you use a sketchline I believe you can make the workplane invisible once it's projected and this way the drawing isn't cluttered with a reference line. You will need to check if this all updates if for example you move the workplane or indeed the offset value. Maybe supply an image and list  of the constraints and dimensions and I can show you a method to get there. Otherwise looking on the forum for examples of linear sketchline dimesions and or here is a not to useful arc dimesion sample, it might give you an idea of the structure.

 

Example constraints could be vertical/horizontal constraint to the workplane center line start and endpoints this way the sketchline is the same length as the centerline. Maybe a parallel constraint between centerline and sketch line and finally an offset dimension. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 17 of 32

malmal02122023
Advocate
Advocate

Hello,


The work planes have constant positions and will not be moved. But the sections should have offset from work planes.

I would like to be able to select the offset and depth of the section via a dialog box.

As for the sketch lines on the main view, they do not interfere because the main view will not be shown in the drawing, only the section views will remain there.


Best regards

 

 

0 Likes
Message 18 of 32

A.Acheson
Mentor
Mentor

Thanks for the information. Can you share an image of how you construct this sketch line constraints and dimensions manually? It will help you understand what constraints and dimensions you need to constrain the sketch line. After all the automation of this is purely based on how you the user want the sketch constrained. I might have a different workflow than you so it is important to figure out how you or your cad team does this in practice. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 19 of 32

malmal02122023
Advocate
Advocate

Please see scheme.

For example:

There are work planes named by 1, 2 and  3

Section line with offset 10 mm from wp '1' and ends of this line with offset 200 and 150 mm from wp '2' and wp '3'

Section looks down

malmal02122023_0-1705418909527.png

malmal02122023_1-1705419090222.png

 

0 Likes
Message 20 of 32

malmal02122023
Advocate
Advocate

Horizontal constrain  was added but I don't know how to add constraints for dimensioning (offset 10 and sides 200 and 150 mm as shown above ao scheme)

And How offseted line should be defined for secion line?

Thanks

 

Sub Main()
    Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument
    
	    Dim oSheet As Sheet
    oSheet = oDoc.ActiveSheet
    
    Dim oFirst_view As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Please select the main view")
    If oFirst_view Is Nothing Then Exit Sub
    Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
    
    Dim occ As ComponentOccurrence
    If oFirst_view.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
    Dim oComp As AssemblyDocument
    oComp = oFirst_view.ReferencedDocumentDescriptor.ReferencedDocument
        
    occ = oComp.ComponentDefinition.Occurrences.ItemByName("Skeleton test:1")
        
    Else
        Exit Sub
    End If
    
    	
    Dim wp As WorkPlane
	
	Dim oSkeletonDoc As PartDocument
	oSkeletonDoc = occ.Definition.Document
	
     
			
		Dim wpList As New List(Of String)

For Each wp In oSkeletonDoc.ComponentDefinition.WorkPlanes
      wpList.Add(wp.Name)
Next
Dim wpSel As String = InputListBox("Work planes found", wpList, "", "Select work plane", "work plane found")
If String.IsNullOrEmpty(wpSel) Then Return
wp = oSkeletonDoc.ComponentDefinition.WorkPlanes(wpSel)

Dim oProxy As WorkPlaneProxy 
occ.CreateGeometryProxy(wp, oProxy) 
oFirst_view.SetIncludeStatus(oProxy, True) 
oFirst_view.SetVisibility(oProxy, True)



 Dim curve1 As DrawingCurve = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCurveSegmentFilter, "Select a drawing Curve").Parent
	Dim centerline As Centerline  = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCenterlineFilter, "Select a centerline")
	Dim drawView As DrawingView = curve1.Parent

	Dim sketch As DrawingSketch = drawView.Sketches.Add
	
	sketch.Edit()

	Dim line1 As SketchLine = sketch.AddByProjectingEntity(centerline)

	Dim line2 As SketchLine = sketch.SketchLines.AddByTwoPoints(line1.Geometry.StartPoint,line1.Geometry.EndPoint)

    
	
	
	Dim oStartPoint As Point2d
	
	oStartPoint = ThisApplication.TransientGeometry.CreatePoint2d(line2.Geometry.StartPoint.X, line2.Geometry.StartPoint.Y+1)
        
    Dim oEndPoint As Point2d
	
	oEndPoint = ThisApplication.TransientGeometry.CreatePoint2d(line2.Geometry.EndPoint.X, line2.Geometry.EndPoint.Y+1)
	
	Dim oLine As SketchLine
            oLine = sketch.SketchLines.AddByTwoPoints(oStartPoint, oEndPoint)
 
           
		
Call sketch.GeometricConstraints.AddHorizontal(oLine)


    sketch.ExitEdit
	
	
Dim oInsertionPoint As Point2d = oTG.CreatePoint2d((oFirst_view.Position.X + 10), (oFirst_view.Position.Y + 10))
Dim oSectionView As SectionDrawingView

oSectionView.SectionLineSketch = oLine

oSectionView = oSheet.DrawingViews.AddSectionView(oFirst_view, sketch, oInsertionPoint, DrawingViewStyleEnum.kHiddenLineDrawingViewStyle)
oSectionView.SetDesignViewRepresentation(oSectionView.DesignViewRepresentation, True)
oSectionView.ReverseDirection

oSectionView.SectionDepth = 50/10
		oSectionView.ReverseDirection
		
		oFirst_view.SetVisibility(oProxy, False)	
  oFirst_view.SetIncludeStatus(oProxy, False) 


End Sub

 

0 Likes