Create Sketch Line from Selected Point

Create Sketch Line from Selected Point

commonmandan
Contributor Contributor
1,769 Views
1 Reply
Message 1 of 2

Create Sketch Line from Selected Point

commonmandan
Contributor
Contributor

What's wrong with the code below? It has a problem making line1A, but not line2. I've mess around quite a bit to try and get it to work. My assumption is that it has something to do with the oPoint0. I've make a longer version of this script that creates about a dozen lines, but the issue is always the first line. What am I misunderstanding about the API and points? 

 

To use this, start a sketch in a part and drawing a line. Run this script and select the end point of the line.

Public Sub Main()
	'Start in an active sketch
	Dim oDrawDoc As PartDocument
	oDrawDoc = ThisApplication.ActiveDocument
	
	Dim oSketches As PlanarSketches
	oSketches = ThisApplication.ActiveDocument.ComponentDefinition.Sketches
	
	Dim oSketch As PlanarSketch
	If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
		oSketch = ThisApplication.ActiveEditObject
	Else
		MessageBox.Show("Need to be in an Active Sketch.", "Title")
		Exit Sub
	End If
	
	selectedStartPoint = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchPointFilter, "Select point")
	
	Dim oPoint0 As Point2d
	'oPoint0 = ThisApplication.TransientGeometry.CreatePoint2d(selectedStartPoint.Geometry.X, selectedStartPoint.Geometry.Y)

	test = False
	Dim startpoint As SketchPoint
	For Each startpoint In oSketch.SketchPoints
		If startpoint.Geometry.IsEqualTo(selectedStartPoint.Geometry)
			oPoint0 = ThisApplication.TransientGeometry.CreatePoint2d(startpoint.Geometry.X, startpoint.Geometry.Y)
			test = True
		End If
	Next
		
	Dim oPoint1A As Point2d
	oPoint1A = ThisApplication.TransientGeometry.CreatePoint2d(line0.EndSketchPoint.Geometry.X + (1 * 2.54), line0.EndSketchPoint.Geometry.Y)

	Dim line1A As SketchLine

	linelA = oSketch.SketchLines.AddByTwoPoints(oPoint0, oPoint1A)
	' ISSUE WITH THE NEXT LINE !!!!!!!
	oSketch.GeometricConstraints.AddHorizontal(line1A)

	Dim oPoint2 As Point2d
	oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(oPoint1A.X, oPoint1A.Y + (1 * 2.54))
	Dim line2 As SketchLine

	' ISSUE WITH THE COMMENTED OUT LINE, B/C line1A IS NOT AN OBJECT?
	'line2 = oSketch.SketchLines.AddByTwoPoints(line1A.EndSketchPoint, oPoint2)
	line2 = oSketch.SketchLines.AddByTwoPoints(oPoint1A, oPoint2)
	MessageBox.Show(line2.Geometry.StartPoint.X, "Title")

	oSketch.GeometricConstraints.AddVertical(line2)

End Sub

 

 

0 Likes
Accepted solutions (1)
1,770 Views
1 Reply
Reply (1)
Message 2 of 2

matt_jlt
Collaborator
Collaborator
Accepted solution

I'm not really sure what some of your code is doing, there are some redundant things like the loop that is in there. Maybe it's just a partial part of your code? When i copied your function i has errors like Line0 not defined.
I wrote something for you that is a more simple method with descriptions on how it works.

Sub Main()
	'Start in an active sketch
	Dim oDrawDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oSketches As PlanarSketches = ThisApplication.ActiveDocument.ComponentDefinition.Sketches
	
	Dim oSketch As PlanarSketch
	If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
		oSketch = ThisApplication.ActiveEditObject
	Else
		MessageBox.Show("Need to be in an Active Sketch.", "Incorrect Active Object")
		Exit Sub
	End If
	
	
	Dim selPoint As SketchPoint = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchPointFilter, "Select point")
	
	' Check an object was selected
	If selPoint Is Nothing Then
		MessageBox.Show("No object selected", "User Selection")
		Exit Sub
	End If
	
	' There are multiple ways to do this, you can create sketch points and use them to create the lines (like i did for pointA) or create lines from 2dPoints not linked to sketch points like i did for PointsB + C
	
	' Create two points needed for line creation
	Dim oPointA As Inventor.SketchPoint = selPoint
	Dim oPointB As Inventor.Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oPointA.Geometry.X, oPointA.Geometry.Y + 2.54)
	Dim oPointC As Inventor.Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oPointA.Geometry.X + 2.54, oPointA.Geometry.Y)
	
	' Ground starting point to lock in place
	oSketch.GeometricConstraints.AddGround(oPointA)
	
	' Create new line object
	Dim oLine1 As Inventor.SketchLine = oSketch.SketchLines.AddByTwoPoints(oPointA,oPointB)
	
	' Add horizontal constraint
	oSketch.GeometricConstraints.AddHorizontal(oLine1)

	Dim oLine2 As Inventor.SketchLine = oSketch.SketchLines.AddByTwoPoints(oPointA, oPointC)
	
	' Add vertical constraint
	oSketch.GeometricConstraints.AddVertical(oLine2)
	
	' Make sketch solve / update
	oSketch.Solve

	MessageBox.Show("Line 2: " & ControlChars.NewLine & "X: " & oLine2.Geometry.StartPoint.X & " | Y: " & oLine2.Geometry.StartPoint.Y, "Line 2 Start Point")


End Sub
0 Likes