Ilogic Rule To Mark Textbox in sketch for Engraving

Ilogic Rule To Mark Textbox in sketch for Engraving

jeanB99VC
Enthusiast Enthusiast
3,034 Views
13 Replies
Message 1 of 14

Ilogic Rule To Mark Textbox in sketch for Engraving

jeanB99VC
Enthusiast
Enthusiast

Hello There,

Im having trouble getting my code right for Mark(ing) (the new function in 2023)  my filenameNumber, to engrave with the laser. The code is working partly, the mark can't be made and I don't know why.

Please see my code below and advise me what is going wrong. I also attached the IPT file which I used.

Thank You in Advance

 

The Error Message i get is:

Error on line 97 in rule: YY_Mark_Tekeningnummer, in document: 97221181-0000 - Markfunction for Engraving.ipt

Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.ObjectCollection'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{6939FFDD-BA10-11D2-B779-0060B0F159EF}' failed due to the following error: Interface wordt niet ondersteund (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

The Code is:

Sub Main()
 
'===================================
'This Is a Ilogic Rule To create a Mark From a textbox For engraving
'It is made with help from internet but NOT WORKING RIGHT !
'I'm getting the message :

'Error On line 90 in rule: YY_Mark_Tekeningnummer, In document: 97221181-0000 - Markfunctie Nummer Filename.ipt

'Unable To cast COM Object Of type 'System.__ComObject' to interface type 'Inventor.ObjectCollection'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{6939FFDD-BA10-11D2-B779-0060B0F159EF}' failed due to the following error: Interface wordt niet ondersteund (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

' PLEASE HELP
'===================================
  
 '  a reference to the currently active document.
 ' This assumes that it is a part document.
	    Dim oPartDoc As PartDocument
	    oPartDoc = ThisApplication.ActiveDocument
	
		Dim oCompDef As PartComponentDefinition
		oCompDef = oPartDoc.ComponentDefinition

  ' Set a reference to the transient geometry object.
    Dim oTransGeom As TransientGeometry
    oTransGeom = ThisApplication.TransientGeometry

'================ Pasted to pick the face where the Mark has to come ============================

'Select Face and Edges to dimension sketch circles from
Dim oFrontFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Text On")

'============================================================

		    ' Create a new sketch on this face
			    oSketch = oCompDef.Sketches.AddWithOrientation(oFrontFace, _
			    oCompDef.WorkAxes.Item(1), True, True, oCompDef.WorkPoints(1))
		
		    ' Determine where in sketch space the point (0.5,0.5,0) is.
			    Dim oCorner As Point2d
			    oCorner = oSketch.ModelToSketchSpace(oTransGeom.CreatePoint(0.5, 0.5, 0))


' ===================== Create a Custom Parameter in Iproperties "Tekeningnummer"======================

		'Creating Custom IProperty with filenumber in it calling it Tekeningnummer
		
			iProperties.Value("Custom", "Tekeningnummer") = ThisDoc.FileName(False) 'false = without extension
			iProperties.Value("Custom", "Tekeningnummer") = (Left(ThisDoc.FileName(False),14))	
		
		'Creating a UserParameter Where the Tekeningnummer value Is put In
		
		Dim PropValue As String = iProperties.Value("Custom", "Tekeningnummer")
		
		'updates the user-defined Text Parameter
		TagName = PropValue
		
'================================ Create a textbox in the Sketch to place the Iporp Tekeningnummer ====================
	 
		'     Create Text With simple String As Input.  Since this doesn't use
		'     any Text Overrides, it will Default To the active Text Style.
		    
			Dim oTG As TransientGeometry
		    oTG = ThisApplication.TransientGeometry
			
			Dim sText As String
		    sText = TagName
		    Dim oTextBox As TextBox
		    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(1, 1), sText)									

'==================== Creating an Objectcollection to be Marked ======================
    Dim oSketchObjects As ObjectCollection
    oSketchObjects = ThisApplication.TransientObjects.CreateObjectCollection
    
    ' Get all entities in the sketch
    Dim oSketchEntity As SketchEntity
    For Each oSketchEntity In oSketch.SketchEntities
        oSketchObjects.Add(oSketchEntity)
    Next

'==================== Creating the Mark on the textbox  ======================

    Dim oMarkFeatures As MarkFeatures
    oMarkFeatures = oCompDef.Features.MarkFeatures
    
    ' Get a mark style.
    Dim oMarkStyle As MarkStyle
    oMarkStyle = oPartDoc.MarkStyles.Item(1)
    
	' Create mark definition.
	Dim oMarkDef As MarkDefinition
	oMarkDef = oMarkFeatures.CreateMarkDefinition(oSketchEntity, oMarkStyle)
	
	' Create a mark feature.
	Dim oMark As MarkFeature
	oMark = oMarkFeatures.Add(oMarkDef)

'====================   
	
End Sub

 

 

 

 

0 Likes
Accepted solutions (2)
3,035 Views
13 Replies
Replies (13)
Message 2 of 14

Michael.Navara
Advisor
Advisor

Quick tip (not tested):

Replace oSketchEntity with oSketchObjects in following line

 

oMarkDef = oMarkFeatures.CreateMarkDefinition(oSketchObjects, oMarkStyle)

 

 

0 Likes
Message 3 of 14

jeanB99VC
Enthusiast
Enthusiast
Hello There,

I've tried this but it still doesn't work. I still get the same Error Message.
Hope to hear if you another solution, or anybody for that manner
0 Likes
Message 4 of 14

Michael.Navara
Advisor
Advisor

My quick answer solves your original question about "Unable to cast COM object of type ..." (Some other errors remain).

But unfortunately it looks like the TextBox is not supported by MarkFeature as valid input geometry in the API.

I try to create new one and also copy existing definition. Both works for MarkFeature with lines but neither works for textbox.

 

0 Likes
Message 5 of 14

jeanB99VC
Enthusiast
Enthusiast
Hello Michael,

Indeed the strange thing is that if you apply the MarkFeature in Inventor it works with the textbox but not through API/Ilogic.
I'v also tried to convert the textbox into geometry but I can't get the code right to do this.
Maybe you know the code to do this and then collect this geometry to mark?
Still I hope that somebody knows how to make the markfeature on a textbox trhough Ilogic/API because of the fact that it is possible in Inventor itself.
0 Likes
Message 6 of 14

johnsonshiue
Community Manager
Community Manager
Accepted solution

Hi Jean,

 

I took a look at the rule. There are two places to be modified. One is the oSketchObjects, which was pointed out earlier. The other one is about how to add Sketch Text to the Sketch Object. Sketch Text isn't considered a Sketch Entity in the API. It is called Text Box. The lines to be rewritten are Line 75 and below. Instead of using Sketch Entity to collect the Sketch Text geometry, TextBox is needed.

 

Dim oSketchText As TextBox
For Each oSketchText In oSketch.TextBoxes
oSketchObjects.Add(oSketchText)
Next

 

Attached is a copy of the working rule, please take a look.

Many thanks!

 



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
Message 7 of 14

WCrihfield
Mentor
Mentor
Accepted solution

Just adding one additional tip in there, to add to what @johnsonshiue just contributed...

When declaring a variable for a TextBox object in iLogic, you should always declare it as 'Inventor.TextBox', instead of simply 'TextBox', because iLogic gets it confused with the System.Windows.Forms.TextBox object if you don't, which can cause problems/errors to happen in your code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 14

jeanB99VC
Enthusiast
Enthusiast
Hi Shiue,
Thank you very much for your solution! 🙂
It works fine.
Now I can finetune it so I can determine the place on the face the Mark should be.
Again Many Thanks!
0 Likes
Message 9 of 14

jeanB99VC
Enthusiast
Enthusiast
Hi WCrihfield,

I have added your remark on de the Invenor.Textbox to the rule from Shiue !
Thank you very much for the help you all gave !
0 Likes
Message 10 of 14

CAD_CAM_MAN
Advocate
Advocate

@johnsonshiue 

I am having difficulty implementing similar code successfully in an add-in I am writing. The code is mostly an exact copy of your attached file.

Everything seems to work as expected until the last line oMark = oMarkFeatures.Add(oMarkDef) is read. 

when the aforementioned line is read the add-in just stops debugging. There is no exception thrown. It does not crash Inventor etc. After the debugging stops I am able to immediately go to the model and manually create a mark feature without issue on the textboxes in the sketch, the  MySketch object is accessing.

 

Any ideas what I may be missing here?

            Dim MySketch As PlanarSketch = BP.ECDef.Sketches.Item(MySketchIndex)
            Dim MyPCDef As PartComponentDefinition = BP.ECDef
            Dim MyDoc As PartDocument = BP.ECDoc

            '==================== Creating an Objectcollection to be Marked ======================
            Dim oSketchObjects As ObjectCollection
            oSketchObjects = g_IApp.TransientObjects.CreateObjectCollection

            ' Get all entities in the sketch
            Dim oSketchText As TextBox
            For Each oSketchText In MySketch.TextBoxes
                oSketchObjects.Add(oSketchText)
            Next

            '==================== Creating the Mark on the textbox  ======================

            Dim oMarkFeatures As MarkFeatures
            oMarkFeatures = MyPCDef.Features.MarkFeatures

            ' Get a mark style.
            Dim oMarkStyle As MarkStyle
            oMarkStyle = MyDoc.MarkStyles.Item(1)

            ' Create mark definition.
            Dim oMarkDef As MarkDefinition
            oMarkDef = oMarkFeatures.CreateMarkDefinition(oSketchObjects, oMarkStyle)

            ' Create a mark feature.
            Dim oMark As MarkFeature
            oMark = oMarkFeatures.Add(oMarkDef)

 

0 Likes
Message 11 of 14

ctx400
Contributor
Contributor

I have the same problem, anyone with a solution?

0 Likes
Message 12 of 14

bbailey6E5PN
Contributor
Contributor

Hello, I am having trouble with this code. Everything works as it should but I cannot get the placement of the mark feature to change after editing values. Am I missing something? Any opinion would be helpful, thank you. 

0 Likes
Message 13 of 14

bbailey6E5PN
Contributor
Contributor

Did you end up figuring out how to change the placement of the feature?

0 Likes
Message 14 of 14

jeanB99VC
Enthusiast
Enthusiast

Hello There,

What do you mean by changing the placement?

The placement is set by clicking the lines. You can change it afterwards by changing the sketch.