Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

WorkPoint_SetByCurveAndEntity (3D Sketch and plane)

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Anonymous
873 Views, 8 Replies

WorkPoint_SetByCurveAndEntity (3D Sketch and plane)

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) 

 

Tags (2)
Labels (2)
8 REPLIES 8
Message 2 of 9
nmunro
in reply to: Anonymous

A couple of things:

 

  • Your code makes reference to 3D Sketch1 whereas in your file, the sketch is named 3D Sketch2
  • You must create the Workpoint since it does not exist in the model. Instead of declaring a workpoint variable and then calling SetByCurveAndEntity, you must add the work point to the model with the corresponding AddByCurveAndEntity method. I used VBA below to accomplish this, you should be able to translate it back to your iLogic code quite easily.
  • Note that the error handling is required (Try-Catch in iLogic) as an exception is raised if their is no intersection.
  • If you have other types of entities (i.e. arcs) in your 3D sketch, you will need to check those as well for the intersection.
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

 

        


https://c3mcad.com

Message 3 of 9
Anonymous
in reply to: nmunro

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.

Message 4 of 9
JhoelForshav
in reply to: Anonymous

@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 🙂

Message 5 of 9
JhoelForshav
in reply to: JhoelForshav

@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
Message 6 of 9
Stakin
in reply to: JhoelForshav

Perfect.efore

before

1201.JPG

after running:

1201A.JPG

Message 7 of 9
Anonymous
in reply to: JhoelForshav

@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

Message 8 of 9
JhoelForshav
in reply to: Anonymous

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
Message 9 of 9
JhoelForshav
in reply to: JhoelForshav

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report