Select line using two points

Select line using two points

Anonymous
Not applicable
921 Views
6 Replies
Message 1 of 7

Select line using two points

Anonymous
Not applicable

Hi,

 

I am trying to select the  sketch line using the two points using API from code in Inventor.

I have a "start point" as (3,5,0) and "end point" as (25,5,0)  of a line and I need to select the line segment on which these two points lies.

 

Can anyone help me to solve my problem?

 

キャプチャ.PNG

 

 

Thank you

 

0 Likes
Accepted solutions (1)
922 Views
6 Replies
Replies (6)
Message 2 of 7

Owner2229
Advisor
Advisor

Hi, try this:

 

Sub Main()
	sPointOneX = 3
	sPointOneY = 5
	sPointTwoX = 25
	sPointTwoY = 5
	' Now we have to convert "mm" to "cm", because Inventor is measuring in "cm"
	sPointOneX = sPointOneX * 0.1
	sPointOneY = sPointOneY * 0.1
	sPointTwoX = sPointTwoX * 0.1
	sPointTwoY = sPointTwoY * 0.1
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
	' Use this line to pick the first sketch in part
	Dim oSketch As Sketch = oCD.Sketches.Item(1)
	' Or use this line to pick the sketch by name
	'Dim oSketch As Sketch = oCD.Sketches.Item("Sketch1")
	Dim oLine As SketchLine
	Dim oNewLine As SketchLine
	For Each oLine In oSketch.SketchLines
		Dim OurLine As Boolean
		If oLine.StartSketchPoint.Geometry.X = sPointOneX And oLine.StartSketchPoint.Geometry.Y = sPointOneY Then
			OurLine = CheckSecond(oLine.EndSketchPoint)
		Else If oLine.StartSketchPoint.Geometry.X = sPointTwoX And oLine.StartSketchPoint.Geometry.Y = sPointTwoY Then
			OurLine = CheckSecond(oLine.EndSketchPoint)
		End If
		If OurLine = True Then
			oNewLine = oLine
			Exit For
		End If
	Next
	If Not oNewLine Is Nothing Then
		MsgBox("We've got the line")
	End If
End Sub

Private sPointOneX As Double
Private sPointOneY As Double
Private sPointTwoX As Double
Private sPointTwoY As Double

Private Function CheckSecond(oPoint As SketchPoint) As Boolean
	If oPoint.Geometry.X = sPointOneX And oPoint.Geometry.Y = sPointOneY Then
		Return True
	Else If oPoint.Geometry.X = sPointTwoX And oPoint.Geometry.Y = sPointTwoY
		Return True
	End If
	Return False
End Function
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 7

Anonymous
Not applicable
Thank you very much Mike.
I will try and let you know 🙂

With Regards,
Prarthana
0 Likes
Message 4 of 7

Anonymous
Not applicable

 

Thank you Mike.

 

I select the sketch line now do you have any idea of getting the dimension name and dimension value 

using start point (3,5,0) and end point(25,5,0) of the selected line on which these two points lies using Inventor API?

 

Regards,

Prarthana

0 Likes
Message 5 of 7

Owner2229
Advisor
Advisor
Accepted solution

Hi, I have no idea what you would need the dimension for, but you can try this:

There's actualy no need to find the sketch line, you just need the points.

 

Sub Main()
	sPointOneX = 3
	sPointOneY = 5
	sPointTwoX = 25
	sPointTwoY = 5
	' Now we have to convert "mm" to "cm", because Inventor is measuring in "cm"
	sPointOneX = sPointOneX * 0.1
	sPointOneY = sPointOneY * 0.1
	sPointTwoX = sPointTwoX * 0.1
	sPointTwoY = sPointTwoY * 0.1
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
	' Use this line to pick the first sketch in part
	Dim oSketch As Sketch = oCD.Sketches.Item(1)
	' Or use this line to pick the sketch by name
	'Dim oSketch As Sketch = oCD.Sketches.Item("Sketch1")
	Dim dCon As DimensionConstraint
	Dim oPoint As Point2D
	Dim oPara As Parameter
	For Each dCon In oSketch.DimensionConstraints
		Dim oPoints As ObjectCollection = dCon.AnchorPoints()
		Dim TryNext As Boolean = False
		Dim OurCon As Boolean = False
		For Each oPoint In oPoints
			If oPoint.X = sPointOneX And oPoint.Y = sPointOneY Then
				If TryNext Then
					OurCon = CheckSecond(oPoint)
				Else
					TryNext = True
				End If
			Else If oPoint.X = sPointTwoX And oPoint.Y = sPointTwoY Then
				If TryNext Then
					OurCon = CheckSecond(oPoint)
				Else
					TryNext = True
				End If
			End If
		Next
		If OurCon = True Then
			oPara = dCon.Parameter
			Exit For
		End If
	Next
	If Not oPara Is Nothing Then
		MsgBox(oPara.Name & vbLf & oPara.Value * 10) 'value multiplicated by 10 to convert units from "cm" to "mm"
	End If
End Sub

Private sPointOneX As Double
Private sPointOneY As Double
Private sPointTwoX As Double
Private sPointTwoY As Double

Private Function CheckSecond(oPoint As Point2d) As Boolean
	If oPoint.X = sPointOneX And oPoint.Y = sPointOneY Then
		Return True
	Else If oPoint.X = sPointTwoX And oPoint.Y = sPointTwoY
		Return True
	End If
	Return False
End Function

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 6 of 7

Anonymous
Not applicable
Dear Mike,

Your code helped me a lot.
Thank you very much.

With Regards,
Prarthana
0 Likes
Message 7 of 7

Owner2229
Advisor
Advisor

You're welcomed 🙂

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods