Inventor Customization

Inventor Customization

Reply
*Expert Elite*
jdkriek
Posts: 545
Registered: ‎03-29-2007
Message 1 of 3 (746 Views)
Accepted Solution

Unable to cast COM object to System.String (iLogic)

746 Views, 2 Replies
03-15-2012 11:24 AM

What I'm trying to do is detect if a Sweep exists - any sweep, not just a specific name (that part works) and then run an iLogic rule if it does. But when I convert to a string to do Feature.IsActive(), then it gives me a "Unable to cast COM object" error. 

 

'Ref the part
Dim oDoc As Inventor.Document oDoc = ThisApplication.ActiveDocument Dim oDef As PartComponentDefinition
'Find the sweep, reguardless of name, and ref it Dim oSweep As SweepFeature oSweep = oDef.Features.SweepFeatures.Item(1)
'This line is throwing the error, because it doesn't like being a string If Feature.IsActive(oSweep) Then iLogicVb.RunRule("CalcSweep") Else 'Do something End If

 

Unable to cast COM object of type 'System.__ComObject' to class type 'System.String'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

How can I make this work?

Jonathan D. Kriek

Autodesk Inventor Certified Expert
Microsoft Certified Application Developer
_____________________________________________________
Did I help you? Please choose Accept as Solution or Kudos below

try this out. 

 

 'Ref the part
Dim oDoc As Inventor.PartDocument = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
'Find the sweep, reguardless of name, and ref it
Dim oSweep As SweepFeature = oDef.Features.SweepFeatures.Item(1)
If Feature.IsActive( oSweep.Name) Then
iLogicVb.RunRule("CalcSweep")
Else
'Do something
End If

 

Valued Mentor
Mike.Wohletz
Posts: 376
Registered: ‎07-29-2008
Message 2 of 3 (728 Views)

Re: Unable to cast COM object to System.String (iLogic)

03-15-2012 06:54 PM in reply to: jdkriek

try this out. 

 

    'Ref the part
        Dim oDoc As Inventor.PartDocument = ThisApplication.ActiveDocument
        Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
        'Find the sweep, reguardless of name, and ref it
        Dim oSweep As SweepFeature = oDef.Features.SweepFeatures.Item(1)
        
        If Feature.IsActive( oSweep.Name) Then
            iLogicVb.RunRule("CalcSweep")
            Else
            'Do something
        End If

 

*Expert Elite*
jdkriek
Posts: 545
Registered: ‎03-29-2007
Message 3 of 3 (722 Views)

Re: Unable to cast COM object to System.String (iLogic)

03-16-2012 05:29 AM in reply to: Mike.Wohletz

Thanks Mike! That works perfect.

 

Makes sense as well, now that I can see the flow.

Jonathan D. Kriek

Autodesk Inventor Certified Expert
Microsoft Certified Application Developer
_____________________________________________________
Did I help you? Please choose Accept as Solution or Kudos below
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.