Accessing the lines in the sketch within the ipt entity

Accessing the lines in the sketch within the ipt entity

hayattangercekler2
Advocate Advocate
742 Views
11 Replies
Message 1 of 12

Accessing the lines in the sketch within the ipt entity

hayattangercekler2
Advocate
Advocate

Hello everyone, happy day. I have an ipt file and in it I have a simple drawing that comes ready on the sketch. I want to access my lines in this drawing. I found a code block. It works with ilogic but not on api. The error I get is not being able to access the ComponentDefinition property of the document. When I do this on the iam file, I can set it as AssemblyDocument and access it. When I try this after opening the ipt entity, it fails. How do I fix this problem and I can access my lines.

 

 

My have code the Ilogic

 

 

 

 

Sub Main()
	sPointOneX = 15
	sPointOneY = 35
	sPointTwoX = 105
	sPointTwoY = 35
	' 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 line3 As Inventor.SketchLine 
	Dim lines = oSketch.SketchLines
	 Dim tg = ThisApplication.TransientGeometry
	Dim oNewLine As SketchLine
	Dim oSketchLine As SketchLine = oSketch.SketchLines.Item(1)
oDoc.SelectSet.Clear
oDoc.SelectSet.Select(oSketchLine)
	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)
			oLine.Delete()
	    
            line3 = lines.AddByTwoPoints(tg.CreatePoint2d(1.5, 2), tg.CreatePoint2d(10.5, 2))
	
		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

 

 

 

 

 

 

0 Likes
Accepted solutions (1)
743 Views
11 Replies
Replies (11)
Message 2 of 12

CattabianiI
Collaborator
Collaborator

> How do I fix this problem and I can access my lines.

Those are two completely different questions, for the second one I would keep posting on the original thread: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/i-want-to-select-desired-line-with-i...

So about the ComponentDefinition error: what do you mean with "after opening the part"? 
try to prompt or log the document full file name: MsgBox(oDoc.FullFileName)

0 Likes
Message 3 of 12

hayattangercekler2
Advocate
Advocate
I couldn't achieve what I wanted to do with the answer given there. Now if I manage to catch the line I think I could use it elsewhere but I haven't tried it. I'm trying to implement the Inventor API according to the ilogic rules I wrote last, but I can't access the "ComponentDefinition" feature in the active document. The error starts directly from here.
0 Likes
Message 4 of 12

CattabianiI
Collaborator
Collaborator

Try to log or messagebox the FullFileName before getting componentDefinition.
What is the exception you get?

0 Likes
Message 5 of 12

hayattangercekler2
Advocate
Advocate

hayattangercekler2_0-1667988626885.png

OK . I did this and got the name of the file.

0 Likes
Message 6 of 12

CattabianiI
Collaborator
Collaborator

And the line after this message box you try to get ComponentDefinition and you get an exception?
What exception?

0 Likes
Message 7 of 12

hayattangercekler2
Advocate
Advocate

hayattangercekler2_1-1667989768651.pnghayattangercekler2_2-1667989782776.png

I'm very new to API. So I tried to try 2 different ways because it worked when I translated it as "AssemblyDocument". There is such an algorithm in all the ilogic codes I found. I can't understand why this feature is not available in API.

 

0 Likes
Message 8 of 12

CattabianiI
Collaborator
Collaborator
Accepted solution

Hi @hayattangercekler2,
that's clear now 🙂

VB.Net and  iLogic allow late binding which means in your case you can call ComponentDefinition on the type Document which does not implement that property. It works at run time (in iLogic o r vbnet addin) because your startDoc variable will be a PartDocument or AssemblyDocument which have the ComponentDefinition property.

So if you are in c# you have to write code in an explicit, faster at runtime but verbose way like this one:

if (StartDoc.DocumentType == DocumentTypeEnum.kPartDocumentObject) {
  var prtDoc = StartDoc as PartDocument;
  var prtCmpDef = prtDoc.ComponentDefinition
}
elseif (StartDoc.DocumentType == DocumentTypeEnum.kPartDocumentObject) {
  var asmDoc = StartDoc as AssemblyDocument;
  var asmCmpDef = asmDoc.ComponentDefinition
} 




0 Likes
Message 9 of 12

hayattangercekler2
Advocate
Advocate
Thank you so much. It worked.
0 Likes
Message 10 of 12

CattabianiI
Collaborator
Collaborator

About the request in the title which could be more useful for other users, post your solution into this other thread: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/i-want-to-select-desired-line-with-i...

0 Likes
Message 11 of 12

hayattangercekler2
Advocate
Advocate
May I ask a slightly off-topic question just to get some brief information? Is it possible to assign a sheet with the Design/Insert Frame tool while in an assembly file using this line I have captured? Or should I open another issue for this?
0 Likes
Message 12 of 12

CattabianiI
Collaborator
Collaborator
make another post
0 Likes