I would like to create a workpoint in each line in the 3d skecth that intersect to the plane.
The code below doesnt work even with the 3d sketch that has only one line.
Please see the attached part file.
Thank you in advance.
Dim oDoc As PartDocument = ThisApplication.ActiveDocument Dim oDef As ComponentDefinition = oDoc.ComponentDefinition Dim oWP As WorkPlane = oDef.WorkPlanes.Item("WorkPlane_Mid") MsgBox(oWP.Name) Dim oSketch As Sketch3D = oDef.Sketches3D.Item("3D Sketch1") MsgBox(oSketch.Name) Dim oWPt As WorkPoint oWPt.SetByCurveAndEntity(oCurve, oWP)
Solved! Go to Solution.
I would like to create a workpoint in each line in the 3d skecth that intersect to the plane.
The code below doesnt work even with the 3d sketch that has only one line.
Please see the attached part file.
Thank you in advance.
Dim oDoc As PartDocument = ThisApplication.ActiveDocument Dim oDef As ComponentDefinition = oDoc.ComponentDefinition Dim oWP As WorkPlane = oDef.WorkPlanes.Item("WorkPlane_Mid") MsgBox(oWP.Name) Dim oSketch As Sketch3D = oDef.Sketches3D.Item("3D Sketch1") MsgBox(oSketch.Name) Dim oWPt As WorkPoint oWPt.SetByCurveAndEntity(oCurve, oWP)
Solved! Go to Solution.
Solved by JhoelForshav. Go to Solution.
Solved by JhoelForshav. Go to Solution.
Solved by JhoelForshav. Go to Solution.
A couple of things:
Public Sub CreateWP()
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition
Set oDef = oDoc.ComponentDefinition
Dim oWP As WorkPlane
Set oWP = oDef.WorkPlanes.Item("WorkPlane_Mid")
Dim oSketch As Sketch3D
Set oSketch = oDef.Sketches3D.Item("3D Sketch2")
Dim oWPt As WorkPoint
Dim i As Integer
Dim line As SketchLine3D
On Error Resume Next
For i = 1 To oSketch.SketchLines3D.Count
Set line = oSketch.SketchLines3D.Item(i)
Set oWPt = oDef.WorkPoints.AddByCurveAndEntity(line, oWP)
If Not oWPt Is Nothing Then
Exit For
End If
Next i
End Sub
A couple of things:
Public Sub CreateWP()
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition
Set oDef = oDoc.ComponentDefinition
Dim oWP As WorkPlane
Set oWP = oDef.WorkPlanes.Item("WorkPlane_Mid")
Dim oSketch As Sketch3D
Set oSketch = oDef.Sketches3D.Item("3D Sketch2")
Dim oWPt As WorkPoint
Dim i As Integer
Dim line As SketchLine3D
On Error Resume Next
For i = 1 To oSketch.SketchLines3D.Count
Set line = oSketch.SketchLines3D.Item(i)
Set oWPt = oDef.WorkPoints.AddByCurveAndEntity(line, oWP)
If Not oWPt Is Nothing Then
Exit For
End If
Next i
End Sub
Thank you @nmunro . The reason why there were two sketches beacuse I tested the code for both simple and a bit complex one.
I also tested your code, looks like the iteration doesnt work because it only created one workpoint. But this is close to what Im looking for.
Thank you @nmunro . The reason why there were two sketches beacuse I tested the code for both simple and a bit complex one.
I also tested your code, looks like the iteration doesnt work because it only created one workpoint. But this is close to what Im looking for.
@Anonymous
The code stops when one point has been created due to this portion of the code
If Not oWPt Is Nothing Then
Exit For
End If
Just remove that and it will keep going 🙂
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
@Anonymous
The code stops when one point has been created due to this portion of the code
If Not oWPt Is Nothing Then
Exit For
End If
Just remove that and it will keep going 🙂
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
@Anonymous
Looking at this I just realized that some curves in a 3D-sketch could intersect the plane multiple times... an arc for example. So calculating each intersection point with transientgeometry and using those as proximitypoints for AddByCurveAndEntity could be a good idea 🙂
See attached ipt.
Dim oDoc As PartDocument = ThisDoc.Document Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition Dim oSketch As Sketch3D = oDef.Sketches3D("3D Sketch1") 'Name of 3D-sketch Dim oPlane As WorkPlane = oDef.WorkPlanes("XZ Plane") 'Name of plane For Each oEnt As SketchEntity3D In oSketch.SketchEntities3D Try For Each oPoint As Point In oPlane.Plane.IntersectWithCurve(oEnt.Geometry) oDef.WorkPoints.AddByCurveAndEntity(oEnt, oPlane, oPoint) Next Catch End Try Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
@Anonymous
Looking at this I just realized that some curves in a 3D-sketch could intersect the plane multiple times... an arc for example. So calculating each intersection point with transientgeometry and using those as proximitypoints for AddByCurveAndEntity could be a good idea 🙂
See attached ipt.
Dim oDoc As PartDocument = ThisDoc.Document Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition Dim oSketch As Sketch3D = oDef.Sketches3D("3D Sketch1") 'Name of 3D-sketch Dim oPlane As WorkPlane = oDef.WorkPlanes("XZ Plane") 'Name of plane For Each oEnt As SketchEntity3D In oSketch.SketchEntities3D Try For Each oPoint As Point In oPlane.Plane.IntersectWithCurve(oEnt.Geometry) oDef.WorkPoints.AddByCurveAndEntity(oEnt, oPlane, oPoint) Next Catch End Try Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
Perfect.efore
before
after running:
Perfect.efore
before
after running:
@JhoelForshav Thank you.
Is it possible to first sort the transient geometry (points) by their x positions before it gets finally generated? Let's say whichever the closest to origin, should be generated first.
In that way, the indices are sorted already. The purpose is to create a sketch3d line using those points
@JhoelForshav Thank you.
Is it possible to first sort the transient geometry (points) by their x positions before it gets finally generated? Let's say whichever the closest to origin, should be generated first.
In that way, the indices are sorted already. The purpose is to create a sketch3d line using those points
Hi @Anonymous
You could add the each Point and SketchEntity to a dictionary, then sort that dictionary with respect to the points distance to the origin. Then traverse that dictionary using the KeyValuePairs of Point, SketchEntity to create the workpoints. Like this 🙂
'---You might need these lines, I don't think so though--- 'AddReference "System.Linq" 'Imports System.Linq '--------------------------------------------------------- Dim oDoc As PartDocument = ThisDoc.Document Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition Dim oSketch As Sketch3D = oDef.Sketches3D("3D Sketch1") 'Name of 3D-sketch Dim oPlane As WorkPlane = oDef.WorkPlanes("XZ Plane") 'Name of plane 'Create a dictionary of Point, SketchEntity Dim oDict As New Dictionary(Of Point, SketchEntity3D) For Each oEnt As SketchEntity3D In oSketch.SketchEntities3D Try For Each oPoint As Point In oPlane.Plane.IntersectWithCurve(oEnt.Geometry) 'Add the Point and SketchEntity to the dictionary oDict.Add(oPoint, oEnt) Next Catch End Try Next 'Sort the dictionary with respect to the distance to origin Dim oOrigin As Point = ThisApplication.TransientGeometry.CreatePoint() 'No arguments = 0,0,0 Dim sorted = From pair In oDict Order By pair.Key.DistanceTo(oOrigin) oDict = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value) 'Create the points from the sorted dictionary For Each oPair As KeyValuePair(Of Point, SketchEntity3D) In oDict oDef.WorkPoints.AddByCurveAndEntity(oPair.Value, oPlane, oPair.Key) Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
Hi @Anonymous
You could add the each Point and SketchEntity to a dictionary, then sort that dictionary with respect to the points distance to the origin. Then traverse that dictionary using the KeyValuePairs of Point, SketchEntity to create the workpoints. Like this 🙂
'---You might need these lines, I don't think so though--- 'AddReference "System.Linq" 'Imports System.Linq '--------------------------------------------------------- Dim oDoc As PartDocument = ThisDoc.Document Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition Dim oSketch As Sketch3D = oDef.Sketches3D("3D Sketch1") 'Name of 3D-sketch Dim oPlane As WorkPlane = oDef.WorkPlanes("XZ Plane") 'Name of plane 'Create a dictionary of Point, SketchEntity Dim oDict As New Dictionary(Of Point, SketchEntity3D) For Each oEnt As SketchEntity3D In oSketch.SketchEntities3D Try For Each oPoint As Point In oPlane.Plane.IntersectWithCurve(oEnt.Geometry) 'Add the Point and SketchEntity to the dictionary oDict.Add(oPoint, oEnt) Next Catch End Try Next 'Sort the dictionary with respect to the distance to origin Dim oOrigin As Point = ThisApplication.TransientGeometry.CreatePoint() 'No arguments = 0,0,0 Dim sorted = From pair In oDict Order By pair.Key.DistanceTo(oOrigin) oDict = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value) 'Create the points from the sorted dictionary For Each oPair As KeyValuePair(Of Point, SketchEntity3D) In oDict oDef.WorkPoints.AddByCurveAndEntity(oPair.Value, oPlane, oPair.Key) Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
Sorry, I just saw that you wrote that you only wanted to use the points X-value. The previous code sorts on distance to origin for the entire point. This sorts with respect to the points absolute X-value (meaning X closest to 0 in this case)
'---You might need these lines, I don't think so though--- 'AddReference "System.Linq" 'Imports System.Linq '--------------------------------------------------------- Dim oDoc As PartDocument = ThisDoc.Document Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition Dim oSketch As Sketch3D = oDef.Sketches3D("3D Sketch1") 'Name of 3D-sketch Dim oPlane As WorkPlane = oDef.WorkPlanes("XZ Plane") 'Name of plane 'Create a dictionary of Point, SketchEntity Dim oDict As New Dictionary(Of Point, SketchEntity3D) For Each oEnt As SketchEntity3D In oSketch.SketchEntities3D Try For Each oPoint As Point In oPlane.Plane.IntersectWithCurve(oEnt.Geometry) 'Add the Point and SketchEntity to the dictionary oDict.Add(oPoint, oEnt) Next Catch End Try Next 'Sort the dictionary with respect to the X-value closest to 0 Dim sorted = From pair In oDict Order By Abs(pair.Key.X) oDict = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value) 'Create the points from the sorted dictionary For Each oPair As KeyValuePair(Of Point, SketchEntity3D) In oDict oDef.WorkPoints.AddByCurveAndEntity(oPair.Value, oPlane, oPair.Key) Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
Sorry, I just saw that you wrote that you only wanted to use the points X-value. The previous code sorts on distance to origin for the entire point. This sorts with respect to the points absolute X-value (meaning X closest to 0 in this case)
'---You might need these lines, I don't think so though--- 'AddReference "System.Linq" 'Imports System.Linq '--------------------------------------------------------- Dim oDoc As PartDocument = ThisDoc.Document Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition Dim oSketch As Sketch3D = oDef.Sketches3D("3D Sketch1") 'Name of 3D-sketch Dim oPlane As WorkPlane = oDef.WorkPlanes("XZ Plane") 'Name of plane 'Create a dictionary of Point, SketchEntity Dim oDict As New Dictionary(Of Point, SketchEntity3D) For Each oEnt As SketchEntity3D In oSketch.SketchEntities3D Try For Each oPoint As Point In oPlane.Plane.IntersectWithCurve(oEnt.Geometry) 'Add the Point and SketchEntity to the dictionary oDict.Add(oPoint, oEnt) Next Catch End Try Next 'Sort the dictionary with respect to the X-value closest to 0 Dim sorted = From pair In oDict Order By Abs(pair.Key.X) oDict = sorted.ToDictionary(Function(p) p.Key, Function(p) p.Value) 'Create the points from the sorted dictionary For Each oPair As KeyValuePair(Of Point, SketchEntity3D) In oDict oDef.WorkPoints.AddByCurveAndEntity(oPair.Value, oPlane, oPair.Key) Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
Can't find what you're looking for? Ask the community or share your knowledge.