Creating Sketch Line on 2d Drawing Using AddByTwoPoints

Creating Sketch Line on 2d Drawing Using AddByTwoPoints

Tiffany_Hayden_
Collaborator Collaborator
2,475 Views
15 Replies
Message 1 of 16

Creating Sketch Line on 2d Drawing Using AddByTwoPoints

Tiffany_Hayden_
Collaborator
Collaborator

Hello. I found this bit of code and tried modifying it to my needs. I'm not sure what I'm missing but it doesn't seem to work. I'm not sure if I'm using GeomIntent Correctly. My workflow is below. Thanks for your help!

 

WorkPoints

WorkPointProxY

Create Sketch

Edit Sketch

Center Marks

CenterMarks to WorkPointProxy

Geometry Intent

CreatePoint2D

Create Line

Constrain Line

 

 

 

                        'Get the first two work points (WorkPoints.Item(1) is the origin)
                        Dim oWorkPoint1 As Inventor.WorkPoint
                        Dim oWP2 As Inventor.WorkPoint
                        Set oWorkPoint1 = GetDocWPt(oSubOcc.Definition.Document, "Center Point")
                        Set oWP2 = GetDocWPt(oSubOcc.Definition.Document, "POINT FLOOR OUT")

                        'Create a proxy for the two work points
                        Dim oWorkPointProx1 As Inventor.WorkPointProxy
                        Dim oWorkPointProx2 As Inventor.WorkPointProxy
                        Call oSubOcc.CreateGeometryProxy(oWorkPoint1, oWorkPointProx1)
                        Call oSubOcc.CreateGeometryProxy(oWP2, oWorkPointProx2)

''                        'Include the work points in the drawing view
'                        Call oView.SetIncludeStatus(WorkPointProx1, True)
'                        Call oView.SetIncludeStatus(oWorkPointProx2, True)
'
'                        oView.SetVisibility oWorkPointProx1, False
'                        oView.SetVisibility oWorkPointProx2, False

                        Dim oDrawingSketch As DrawingSketch
                        Dim oLine As SketchLine
                        Set oDrawingSketch = oView.Sketches.Add
                        Call oDrawingSketch.Edit



                        'Now we need to find the two centermarks that represent the work point proxies
                        Dim oCenterMark1 As Inventor.Centermark
                        Dim oCenterMark2 As Inventor.Centermark
                        Dim oCenterMark As Inventor.Centermark
                        For Each oCenterMark In oSheet.Centermarks
                            If oCenterMark.Attached Then
                                If oCenterMark.AttachedEntity Is oWorkPointProx1 Then
                                    Set oCenterMark1 = oCenterMark
                                ElseIf oCenterMark.AttachedEntity Is oWorkPointProx2 Then
                                    Set oCenterMark2 = oCenterMark
                                End If
                            End If
                        Next

                        Dim oTG As TransientGeometry: Set oTG = ThisApplication.TransientGeometry


                        'From the two work points, we create the geometry intent
                        Dim oGeomIntent1 As Inventor.GeometryIntent
                        Dim oGeomIntent2 As Inventor.GeometryIntent
                        Set oGeomIntent1 = oSheet.CreateGeometryIntent(oCenterMark1, Inventor.kPoint2dIntent)
                        Set oGeomIntent2 = oSheet.CreateGeometryIntent(oCenterMark2, Inventor.kPoint2dIntent)


                        Dim oPointOne As Point2d
                        Set oPointOne = oTG.CreatePoint2d(oGeomIntent1.PointOnSheet.X, oGeomIntent1.PointOnSheet.Y)
                        Dim oPointTwo As Point2d
                        Set oPointTwo = oTG.CreatePoint2d(oGeomIntent2.PointOnSheet.X, oGeomIntent2.PointOnSheet.Y)

                        Dim oPointStart As Point2d
                        Set oPointStart = oView.SheetToDrawingViewSpace(oPointOne)

                        Dim oPointEnd As Point2d
                        Set oPointEnd = oView.SheetToDrawingViewSpace(oPointTwo)


                        Set oLine = oDrawingSketch.SketchLines.AddByTwoPoints(oPointStart, oPointEnd)
                        oLine.Construction = True

                        Call oDrawingSketch.GeometricConstraints.AddHorizontal(oLine)
Call oDrawingSketch.ExitEdit

 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Accepted solutions (2)
2,476 Views
15 Replies
Replies (15)
Message 2 of 16

yan.gauthier
Advocate
Advocate
Accepted solution

Hi,

 

Have you tried using oPointOne and oPointTwo instead ?

 

Regards,

 

 

0 Likes
Message 3 of 16

Tiffany_Hayden_
Collaborator
Collaborator

@yan.gauthier  Yes. I still get this error saying oPoint1 and oPoint2 is nothing. I'm not sure why they are nothing. My Geom is nothing too in the Locals window. But it found something? 

 

oPoint1oPoint2.PNG

 

geom.PNG

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 4 of 16

yan.gauthier
Advocate
Advocate

Hi,

 

I did a quick check and I think you could just use your centermark.PointOnSheet.X and centermark.PointOnSheet.Y instead ?

 

a Geometry intent would be required for you to create a centermark or an annotation, you don't need it to create a sketch element.

 

Cheers,

0 Likes
Message 5 of 16

Tiffany_Hayden_
Collaborator
Collaborator

Like this? 

 

                        Dim oPointOne As Point2d
                        Set oPointOne = oTG.CreatePoint2d(oCenterMark1.PointOnSheet.X, oCenterMark1.PointOnSheet.Y)
                        Dim oPointTwo As Point2d
                        Set oPointTwo = oTG.CreatePoint2d(oCenterMark2.PointOnSheet.X, oCenterMark2.PointOnSheet.Y)

                        Dim oPointStart As Point2d
                        Set oPointStart = oView.SheetToDrawingViewSpace(oPointOne)

                        Dim oPointEnd As Point2d
                        Set oPointEnd = oView.SheetToDrawingViewSpace(oPointTwo)


                        Set oLine = oDrawingSketch.SketchLines.AddByTwoPoints(oPointOne, oPointTwo)
                        oLine.Construction = True

                        Call oDrawingSketch.GeometricConstraints.AddHorizontal(oLine)

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 6 of 16

yan.gauthier
Advocate
Advocate

yes and like this, you pointstart and pointend are not required

 

here is a macro i made to draw a vertical and an horizontal line to separate the folded view from the unfolded view at my job. After the macro run, the user may remove one or the other sketch. As long as there is still one of the sketch there, the macro won't fire again

 

Sub AddPliageLine(osheet As Sheet)

'Create vertical line and horizontal between titleblock edge and sheet border
Dim invapp As Inventor.Application
Dim oBorder As Border
Dim oTitleBlock As TitleBlock
Dim oLine As LineSegment
Dim oSketch As Sketch
Dim HighPoint As Point2d
Dim LowPoint As Point2d
Dim LeftPoint As Point2d
Dim RightPoint As Point2d

On Error GoTo AddPliageLineErr

Set invapp = ThisApplication

'Look for already existing sketch
For Each oSketch In osheet.Sketches
    If InStr(1, oSketch.name, "vertical", vbTextCompare) > 0 Or InStr(1, oSketch.name, "horizontal", vbTextCompare) > 0 Then
        Exit Sub 'Sketch already there, don't add another
    End If
Next oSketch

'Validate there is a title block
If Not osheet.TitleBlock Is Nothing Then
    Set oTitleBlock = osheet.TitleBlock
Else
    MsgBox "Le dessin doit contenir un cartouche", vbMsgBoxSetForeground, "AddDecoupeLine"
    Exit Sub
End If

'Validate there is a sheet border
If Not osheet.Border Is Nothing Then
    Set oBorder = osheet.Border
Else
    MsgBox "Le dessin doit contenir une bordure", vbMsgBoxSetForeground, "AddDecoupeLine"
    Exit Sub
End If

'Set start and end points of lines
Set LowPoint = invapp.TransientGeometry.CreatePoint2d(osheet.Width / 2, oBorder.RangeBox.MinPoint.Y)
Set HighPoint = invapp.TransientGeometry.CreatePoint2d(osheet.Width / 2, oBorder.RangeBox.MaxPoint.Y)
Set LeftPoint = invapp.TransientGeometry.CreatePoint2d(oBorder.RangeBox.MinPoint.x, osheet.Height / 2)
Set RightPoint = invapp.TransientGeometry.CreatePoint2d(oBorder.RangeBox.MaxPoint.x, osheet.Height / 2)


'Create Vertical line
Set oSketch = osheet.Sketches.add
oSketch.name = "Vertical"
oSketch.Edit
    oSketch.SketchLines.AddByTwoPoints LowPoint, HighPoint
oSketch.ExitEdit

'Create Horizontal line
Set oSketch = osheet.Sketches.add
oSketch.name = "Horizontal"
oSketch.Edit
    oSketch.SketchLines.AddByTwoPoints LeftPoint, RightPoint
oSketch.ExitEdit

AddPliageLineErr:
If Err Then
    MsgBox "Unexpected error during AddPliageLine routine: " & Err.Description, vbMsgBoxSetForeground + vbCritical, "AddPliageLine"
    Err.Clear
End If

End Sub

 

Cheers, 

0 Likes
Message 7 of 16

Tiffany_Hayden_
Collaborator
Collaborator

@yan.gauthier  Awesome sub! I'll try it out and see where I get. Thank you! 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 8 of 16

Tiffany_Hayden_
Collaborator
Collaborator

@yan.gauthier 

I tried to run your code but it didn't work. 

 

I think the issue is my x and y coordinate is in model space. Do I need to add anything different to the code when referencing .X and .Y. I noticed that wasn't a choice in the intelisense. 

                        Dim StartPoint As Point2d
                        Dim EndPoint As Point2d
                        
                        Set StartPoint = ThisApplication.TransientGeometry.CreatePoint2d(GetDocWPt(oSubOcc.Definition.Document, "Center Point").X, GetDocWPt(oSubOcc.Definition.Document, "Center Point").Y)
                        Set EndPoint = ThisApplication.TransientGeometry.CreatePoint2d(GetDocWPt(oSubOcc.Definition.Document, "POINT FLOOR OUT").X, GetDocWPt(oSubOcc.Definition.Document, "POINT FLOOR OUT").Y)
                        
                        'Create Horizontal line
                        Dim osketch As DrawingSketch
                        Set osketch = oSheet.Sketches.Add
                        osketch.Name = "Horizontal"
                        osketch.Edit
                            osketch.SketchLines.AddByTwoPoints StartPoint, EndPoint
                        osketch.ExitEdit

 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 9 of 16

Tiffany_Hayden_
Collaborator
Collaborator

Nevermind. missed adding .point! Thank you. 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 10 of 16

Tiffany_Hayden_
Collaborator
Collaborator

@yan.gauthier  Well good news it's not erroring out but it's also not giving me what I'm wanting. Running this code It's creating two a line from 0,0 bottom left of sheet and making a vertical line. I'm not sure exactly why it's doing that but it might be because I'm referencing the points in model space. Is there a conversion that has to be made? Like scale or something? 

 

lines.PNG

 

                        Dim StartPoint As Point2d
                        Dim EndPoint As Point2d
                        
                        Set StartPoint = ThisApplication.TransientGeometry.CreatePoint2d(GetDocWPt(oSubOcc.Definition.Document, "Center Point").Point.X, GetDocWPt(oSubOcc.Definition.Document, "Center Point").Point.Y)
                        Set EndPoint = ThisApplication.TransientGeometry.CreatePoint2d(GetDocWPt(oSubOcc.Definition.Document, "POINT FLOOR OUT").Point.X, GetDocWPt(oSubOcc.Definition.Document, "POINT FLOOR OUT").Point.Y)
                        
                        'Create Horizontal line
                        Dim osketch As DrawingSketch
                        Set osketch = oSheet.Sketches.Add
'                        osketch.Name = "Horizontal"
                        osketch.Edit
                            osketch.SketchLines.AddByTwoPoints StartPoint, EndPoint
                            osketch.SketchLines.Item(1).LineType = kDoubleDashedDottedLineType
                        osketch.ExitEdit

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 11 of 16

yan.gauthier
Advocate
Advocate
Accepted solution

Hi,

 

I did some digging and apparently, you should create geometry proxies in your top assembly using the proxies you created in your sub occurrences.

 

Then, you can use the Sheet.Centermarks.AddByWorkFeature(GeometryProxy, View)

 

Solution was found here :

 

https://forums.autodesk.com/t5/inventor-customization/creating-a-centermark-from-a-workpoint-of-a-su...

 

Cheers,

0 Likes
Message 12 of 16

Tiffany_Hayden_
Collaborator
Collaborator

@yan.gauthierThanks! I'll try it out and let you know. Appreciate the digging. I dug all day yesterday! 🙂 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

Message 13 of 16

Tiffany_Hayden_
Collaborator
Collaborator

 @yan.gauthier Got the points coming in. But the line isn't working. When I add this it errors on this line. 

 

lines2.PNG

 

                        Dim oWorkPoint1 As Inventor.WorkPoint
                        Dim oWP2 As Inventor.WorkPoint
                        Set oWorkPoint1 = GetDocWPt(oSubOcc.Definition.Document, "Center Point")
                        Set oWP2 = GetDocWPt(oSubOcc.Definition.Document, "POINT FLOOR OUT")
'
'                        'Create a proxy for the two work points
                        Dim oWorkPointProx1 As Inventor.WorkPointProxy
                        Dim oWorkPointProx2 As Inventor.WorkPointProxy
                        Call oSubOcc.CreateGeometryProxy(oWorkPoint1, oWorkPointProx1)
                        Call oSubOcc.CreateGeometryProxy(oWP2, oWorkPointProx2)
                        
                        
                        Dim oWorkPointAssyProx1 As Inventor.WorkPointProxy
                        Call oCompOcc.CreateGeometryProxy(oWorkPointProx1, oWorkPointAssyProx1)
                        Dim oWorkPointAssyProx2 As Inventor.WorkPointProxy
                        Call oCompOcc.CreateGeometryProxy(oWorkPointProx2, oWorkPointAssyProx2)
                        
                        Dim oCenterMarks1 As Centermarks
                        Dim oCenterMarks2 As Centermarks
                        Set oCenterMarks1 = oSheet.Centermarks
                        Set oCenterMarks2 = oSheet.Centermarks
                        
                        Dim oCenterMark1 As Centermark
                        Dim oCenterMark2 As Centermark
                        Set oCenterMark1 = oCenterMarks1.AddByWorkFeature(oWorkPointAssyProx1, oView)
                        Set oCenterMark2 = oCenterMarks2.AddByWorkFeature(oWorkPointAssyProx2, oView)
                        
                        Dim oSketch As DrawingSketch
                        Set oSketch = oSheet.Sketches.Add
                        oSketch.Edit
                            oSketch.SketchLines.AddByTwoPoints oCenterMark1, oCenterMark2
                            oSketch.SketchLines.Item(1).LineType = kDoubleDashedDottedLineType



                        oSketch.ExitEdit

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 14 of 16

yan.gauthier
Advocate
Advocate

Use the centermarks to create your two point2d 😉

0 Likes
Message 15 of 16

Tiffany_Hayden_
Collaborator
Collaborator

OOHHH!!! Okay. Thank you! 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

Message 16 of 16

Tiffany_Hayden_
Collaborator
Collaborator

It works now!! Yay!! 

 

What I had wrong for some time was using CenterMark.Position.X and Y. It was stuck there. 🙂 Thanks for all your help!! 

 

For reference this is the code that worked. This is for oOcc and oSubOcc. For loop. 

Yay! I'm so pumped. 🙂 Thanks again. 

 

                        'Get the first two work points (WorkPoints.Item(1) is the origin)
                        Dim oWorkPoint1 As Inventor.WorkPoint
                        Dim oWorkPoint2 As Inventor.WorkPoint
                        Set oWorkPoint1 = GetDocWPt(oSubOcc.Definition.Document, "Center Point")
                        Set oWorkPoint2 = GetDocWPt(oSubOcc.Definition.Document, "POINT FLOOR OUT")
'
'                        'Create a proxy for the two work points
                        Dim oWorkPointProx1 As Inventor.WorkPointProxy
                        Dim oWorkPointProx2 As Inventor.WorkPointProxy
                        Call oSubOcc.CreateGeometryProxy(oWorkPoint1, oWorkPointProx1)
                        Call oSubOcc.CreateGeometryProxy(oWorkPoint2, oWorkPointProx2)
                        
                        
                        Dim oWorkPointAssyProx1 As Inventor.WorkPointProxy
                        Call oCompOcc.CreateGeometryProxy(oWorkPointProx1, oWorkPointAssyProx1)
                        Dim oWorkPointAssyProx2 As Inventor.WorkPointProxy
                        Call oCompOcc.CreateGeometryProxy(oWorkPointProx2, oWorkPointAssyProx2)
                        
                        Dim oCenterMarks1 As Centermarks
                        Dim oCenterMarks2 As Centermarks
                        Set oCenterMarks1 = oSheet.Centermarks
                        Set oCenterMarks2 = oSheet.Centermarks
                        
                        Dim oCenterMark1 As Centermark
                        Dim oCenterMark2 As Centermark
                        Set oCenterMark1 = oCenterMarks1.AddByWorkFeature(oWorkPointAssyProx1, oView)
                        Set oCenterMark2 = oCenterMarks2.AddByWorkFeature(oWorkPointAssyProx2, oView)

                        Dim oGeomIntent1 As Inventor.GeometryIntent
                        Dim oGeomIntent2 As Inventor.GeometryIntent
                        Set oGeomIntent1 = oSheet.CreateGeometryIntent(oCenterMark1, Inventor.kPoint2dIntent)
                        Set oGeomIntent2 = oSheet.CreateGeometryIntent(oCenterMark2, Inventor.kPoint2dIntent)
                        
                        Dim oTG As TransientGeometry
                        Set oTG = ThisApplication.TransientGeometry
                        
                        Dim oPointOne As Point2d
                        Set oPointOne = oTG.CreatePoint2d(oCenterMark1.Position.X, oCenterMark1.Position.Y)
                        Dim oPointTwo As Point2d
                        Set oPointTwo = oTG.CreatePoint2d(oCenterMark2.Position.X, oCenterMark2.Position.Y)
                        
                        
                        Dim oSketch As DrawingSketch
                        Set oSketch = oSheet.Sketches.Add

                        Dim oLine As SketchLine

                        oSketch.Edit
                        Set oLine = oSketch.SketchLines.AddByTwoPoints(oPointOne, oPointTwo)
                        oSketch.SketchLines.Item(1).LineType = kDoubleDashedDottedLineType



                        oSketch.ExitEdit

 

 

 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature