iLogic for idw from iam

iLogic for idw from iam

dan_inv09
Advisor Advisor
3,278 Views
32 Replies
Message 1 of 33

iLogic for idw from iam

dan_inv09
Advisor
Advisor

It seems when it gets to this line:

 

oBaseView = oSheet.DrawingViews.AddBaseView(oAsmCompDef,oPoint,2,kFrontViewOrientation,kHiddenLineRemovedDrawingViewStyle,,,oBaseViewOptions)

 

it gives this error:

 

Error in rule: Rule0, in document: DUMMY WELDMENT.iam

Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor._Document'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{70109AA1-63C1-11D2-B78B-0060B0EC020B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

 

Now, let's start off by stating that I have absolutely no idea what I'm doing. I'm just grabbing bits and pieces from other peoples code and trying to reconcile the variables. If someone could just point me in the direction of some sort of syntax so I can figure out what is supposed to be in that statement, that would be great, but I look at it and it all seems like gobbledygook and maybe the error is saying that it is.

 

I'll attach the whole code if anyone wants to point and laugh. (But if you had some constructive criticism that'd be okay too.)

 

I've got something misplaced or misspelled or something but I really don't know what it's supposed to look like, so I could really use some help. If anyone can even just tell me where to go to find out what these commands are supposed to look like it would be a big help.

 

Thank you for taking the time to assist me. I really appreciate it.

0 Likes
3,279 Views
32 Replies
Replies (32)
Message 2 of 33

Owner2229
Advisor
Advisor

Hi, it says it wants a document, but you're throwing it a ComponentDefinition, so try to feed it with this:

 

oBaseView = oSheet.DrawingViews.AddBaseView(oAsmDoc, oPoint, 2, kFrontViewOrientation, kHiddenLineRemovedDrawingViewStyle, , , oBaseViewOptions)

 

 

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
0 Likes
Message 3 of 33

dan_inv09
Advisor
Advisor

Thank you very much for helping but we're still not quite there.

 

Now it gives me:

 

Error in rule: Rule0, in document: DUMMY WELDMENT.iam

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

under the More Info tab:

 

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.DrawingViews.AddBaseView(_Document Model, Point2d Position, Double Scale, ViewOrientationTypeEnum ViewOrientation, DrawingViewStyleEnum ViewStyle, String ModelViewName, Object ArbitraryCamera, Object AdditionalOptions)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

 

So it looks to me like oAsmDoc might not strictly be the "Model" or something?

 

I'll look more after lunch.

0 Likes
Message 4 of 33

Anonymous
Not applicable

It looks like your for each loop is wanting to place a view for each part in the assembly. Try this

 

oBaseView = oSheet.DrawingViews.AddBaseView(docFile,oPoint,2,kFrontViewOrientation,kHiddenLineRemovedDrawingViewStyle,,,oBaseViewOptions)

0 Likes
Message 5 of 33

dan_inv09
Advisor
Advisor

Yes, that is what I'm trying to do. I found this code to make a design view representation for each part in an assembly where only the first instance of that part is the only thing visible. Now I'm trying to modify it to create a drawing view using each of those design views.

 

Trying that gave:

 

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.DrawingViews.AddBaseView(_Document Model, Point2d Position, Double Scale, ViewOrientationTypeEnum ViewOrientation, DrawingViewStyleEnum ViewStyle, String ModelViewName, Object ArbitraryCamera, Object AdditionalOptions)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

which I think might be the same error as when I tried

 

oBaseView = oSheet.DrawingViews.AddBaseView(oAsmDoc, oPoint, 2, kFrontViewOrientation, kHiddenLineRemovedDrawingViewStyle, , , oBaseViewOptions)

 

 

so either something else is what's wrong or oAsmDoc and docFile are wrong in the same way, or something?

0 Likes
Message 6 of 33

Anonymous
Not applicable

I converted this to vba so I could step thru it.

I commented out the code that was creating the different assembly views becuase it was not working correctly.

the portion that places a view for each part in an assembly now works.

 

Public Sub MakeViewForEachPart()

'create a design view representation for each unique part in the assembly
Dim oAsmDoc As AssemblyDocument
  Set oAsmDoc = ThisApplication.ActiveDocument
  Dim sPath As String
  sPath = oAsmDoc.FullFileName

'set a reference to the assembly component definintion.
'this assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'look at all of the components in the assembly
Dim oCompDef As Inventor.ComponentDefinition
Set oCompDef = oAsmDoc.ComponentDefinition

'define the first level components collection
Dim oCompOcc As Inventor.ComponentOccurrence

'define view rep
Dim oViewRep As DesignViewRepresentation

'define an arraylist to hold the list of  view rep names
'Dim NameList As Object
'Set NameList = CreateObject("System.Collections.ArraList")


'Look at the view reps in the assembly
'For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations
''set the list of names to the array list
'Call NameList.Add(oViewRep.Name)
'Next

'check for a Default view rep and create it if not found
'If Not NameList.Contains("Default") Then
'    'create Default view rep
'    oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add("Default")
'    oViewRep.ShowAll
'    oViewRep.Activate
'End If

'zoom all
'ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute

        ' Create a new NameValueMap object
Dim oBaseViewOptions As NameValueMap
Set oBaseViewOptions = ThisApplication.TransientObjects.CreateNameValueMap

'create drawing
  Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kDrawingDocumentObject))
        Dim oSheet As Sheet
        Set oSheet = oDrawDoc.ActiveSheet


'look at all of the unique parts in the assembly
For Each docFile In oAsmDoc.AllReferencedDocuments
    If docFile.DocumentType = 12290 Then '12290 is the part document enumurator
    'locate the last backslash position in the full file name
    Dim FNamePos As Long
    FNamePos = InStrRev(docFile.FullFileName, "\", -1)
    'remove path from part file name
    Dim docFName As String
    docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
    'remove extension from part file name
    ShortName = Left(docFName, Len(docFName) - 4)

    'Set oFile = ThisDoc.Path

'        'check to see if the arraylist contains the desired view rep
'        If Not NameList.Contains(ShortName) Then
'            'create new View Rep
'            oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Add(ShortName)
'            oViewRep.Activate
'            oViewRep.Locked = False
'        ElseIf NameList.Contains(ShortName) Then
'            'reference existing View Rep
'            oViewRep = oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item(ShortName)
'            oViewRep.Activate
'            oViewRep.Locked = False
'        End If

        'look at all of the occurences
'        For Each oCompOcc In oCompDef.Occurrences
'            'locate the colon position in the occurence name
'            oCompOccPos = InStrRev(oCompOcc.Name, ":")
'            'set occurence name to everything left of the colon
'            oOccName = Left(oCompOcc.Name, oCompOccPos - 1)
'
'                'set visible if name matches first occurence
'                If oCompOcc.Name = ShortName & ":1" Then
'                    oCompOcc.Visible = True
'                   'ThisApplication.ActiveView.Update()
'                Else
'                    oCompOcc.Visible = False
'                    'ThisApplication.ActiveView.Update()
'                End If
'        Next
    End If
    
                'create drawing view

                Set oSheet = oDrawDoc.ActiveSheet
                'Call oBaseViewOptions.Add("DesignViewRepresentation", oViewRep.Name)
            
                'Call oBaseViewOptions.Add("DesignViewAssociative", True)
                ' Create the placement point object.

                Dim oPoint As Point2d

                Set oPoint = ThisApplication.TransientGeometry.CreatePoint2d(25, 25)

                ' Create a base view.
                Dim oBaseView As DrawingView

                Set oBaseView = oSheet.DrawingViews.AddBaseView(docFile, oPoint, 2, kFrontViewOrientation, kHiddenLineRemovedDrawingViewStyle, , , oBaseViewOptions)
  
    
                'lock view rep
                'oViewRep.Locked = True
 
Next

'set Default View Rep active
'oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Default").Activate


End Sub
0 Likes
Message 7 of 33

dan_inv09
Advisor
Advisor

Okay, I'm not trying to place a view of each part. I want an assembly view showing each part. That's why the assembly views part is important.

I need to be able to manipulate the BOM and have all the views on the drawing reflect that but the parts don't know anything about the assembly.

 

So thinking along those lines I commented out all the drawing stuff in the loop and it still works for creating the design views. But when I only commented out the AddBaseView line I get

 

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.Documents.get_Item(Int32 Index)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

which is exactly the same except for

 

at Inventor.Documents.get_Item(Int32 Index)

 

where

 

at Inventor.DrawingViews.AddBaseView(_Document Model, Point2d Position, Double Scale, ViewOrientationTypeEnum ViewOrientation, DrawingViewStyleEnum ViewStyle, String ModelViewName, Object ArbitraryCamera, Object AdditionalOptions)

 

was. So what the heck does that mean?

 

I'm wondering if I should try taking the original view rep code and go after it with the view placement stuff. I don't think I should duplicate the loop - it goes through every part ... but if I take the bit where it works with the first occurrence and put the drawing view stuff in there ... ? I would think it would be better if I could figure out how to make a new array of the design views and do the "for each" from that, I think.

0 Likes
Message 8 of 33

dan_inv09
Advisor
Advisor

I don't know if I should start a new thread, this one should keep going because we have not yet figured out why AddBaseView is breaking, but now I've found a new issue.

It's hanging up on the parent of a derived part. I commented out the AddBaseView and everything else is okay for the first part until

 

SyntaxEditor Code Snippet

Call oBaseViewOptions.Add("DesignViewRepresentation", oViewRep.Name)

for the parent of the derived part. You see, I made the assembly with Make Components from a part I made with a bunch of different solid bodies.

 

Now this particular code I was using with subassemblies before and I noticed that while it is able to find every single part within the assembly it's run in and make a design view named after them, it cannot control the visiblity within the subassembly. But I do not see how the BaseViewOptions would kick out on an empty view - I would think it would have to be the placement of the empty view that causes the problems.

 

So I've got to find some way to skip that one now - as well as figure out how to get the views of the parts in the assembly into a drawing.

0 Likes
Message 9 of 33

dan_inv09
Advisor
Advisor

Is this "Call BaseViewOptions" the right way to do this?

I thought I may have seen something where view rep was part of the regular AddBaseView.

 

Can someone help with the syntax for AddBaseView?

 

We've got:

Component Definition

location

scale

view orientation

hidden line style

two I haven't seen yet

and then the example I've been working from has this oBaseViewOptions which is a NameValueMap object

 

I wonder if the oBaseViewOptions could be where the problem comes from.

 

I started messing with the code to create the Design View Reps in the assembly because I thought it might be easier to just create drawing views as the reps were created. Does anyone think it might be easier to place a drawing view for the assembly view reps in a separate program?

 

0 Likes
Message 10 of 33

adam.nagy
Autodesk Support
Autodesk Support

Hi,

 

So there are two parts in your code, which you could also separate to make things simpler:

1) Creating Design View representations in the assembly

2) Placing views of the assembly based on each design view representation (DVR)

 

Does the first part work OK for you?

 

Once I have the correct DVR's in my assembly...

DVRs.png

 

... I can add a view for them using this code:  

Sub InsertDesignViewRep()
  Dim dwg As DrawingDocument
  Set dwg = ThisApplication.ActiveDocument
  
  Dim asm As AssemblyDocument
  Set asm = ThisApplication.Documents.Open( _
    "\\psf\Home\Documents\Inventor\Holes.iam", False)
  
  Dim s As Sheet
  Set s = dwg.ActiveSheet
  
  Dim tro As TransientObjects
  Set tro = ThisApplication.TransientObjects
  
  Dim op As NameValueMap
  Set op = tro.CreateNameValueMap()
  
  Dim trg As TransientGeometry
  Set trg = ThisApplication.TransientGeometry
  
  Call op.Add("DesignViewRepresentation", "Holes")
  
  Call s.DrawingViews.AddBaseView( _
    asm, trg.CreatePoint2d(), 1, _
    kTopViewOrientation, kHiddenLineRemovedDrawingViewStyle, _
    , , op)
End Sub

 

Cheers,



Adam Nagy
Autodesk Platform Services
0 Likes
Message 11 of 33

dan_inv09
Advisor
Advisor

 

It tells me:

 

Error in rule program format:

All other Sub's or Functions must be after Sub Main()

 

So I change the first line to: 

Sub Main()

 and it gives "Rule Compile Errors": "'Let' and 'Set' assignment statements are no longer supported" for six different lines.

0 Likes
Message 12 of 33

adam.nagy
Autodesk Support
Autodesk Support

Have a look at this:

http://adndevblog.typepad.com/manufacturing/2015/11/convert-vba-to-net-ilogic.html



Adam Nagy
Autodesk Platform Services
0 Likes
Message 13 of 33

dan_inv09
Advisor
Advisor

And now that I'm looking at it, not that I'm not thankful for the help or anything but:

 

Okay, I suppose I could live with having to add the assembly file name every time. We don't necessarily need it automatically find the active assembly.

Typing the view name into the code, however, is not acceptable. We are averaging 20-30 view reps, we need to use the code to cycle through all the view reps in the assembly and create the views in the drawing. If we expect someone to type the names in the code it would be much, much faster to create the views without the code.

 

And the code I have to create the view reps in the assembly is iLogic. So, I was trying to create the drawing views using iLogic. But the only help I can find for creating the drawing views seems to be vba. I think the whole reason the "AddBaseView" statement is giving me problems might be because it's vba not iLogic.

 

Could anyone please help find the proper way to create a drawing view for a design view rep using iLogic?

0 Likes
Message 14 of 33

dan_inv09
Advisor
Advisor
0 Likes
Message 15 of 33

dan_inv09
Advisor
Advisor

 

Syntax

DrawingViews.AddBaseView( Model As Document, Position As Point2d, Scale As Double, ViewOrientation As ViewOrientationTypeEnum, ViewStyle As DrawingViewStyleEnum, [ModelViewName] As String, [ArbitraryCamera] As Variant, [AdditionalOptions] As Variant ) As DrawingView

 

 

ModelViewName Optional input String that defines the design view name for assembly files, or the presentation view name for presentation files. This argument is ignored if the document type specified by the Model argument is a part file (.ipt).

 

 

So all this time we could have been putting design view in the statement instead of messing around with the oBaseViewOptions NameValueMap. It will not set the view as associative, but there is probably a way to flip through all the views and make them associative with a separate iLogic rule (I've got one that works for changing scale - there might be a way to make it work for that).

 

I'll try this with my dummy "weldment" (my actual work involves multiple assemblies with mostly common parts which I would put into one parent assembly to run this on ... except although the code that picks the names of the design views cares only if there is a part anywhere, the code that sets the visibility can not dig into the subassemblies.)

0 Likes
Message 16 of 33

dan_inv09
Advisor
Advisor

Okay, I'm still getting:

 

Error in rule: Rule0, in document: DUMMY WELDMENT.iam

Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor._Document'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{70109AA1-63C1-11D2-B78B-0060B0EC020B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor._Document'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{70109AA1-63C1-11D2-B78B-0060B0EC020B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

 

I wish I knew what that means.

0 Likes
Message 17 of 33

dan_inv09
Advisor
Advisor

In the comments of this video

 

https://www.youtube.com/watch?v=kVerDyVskQ0

 

someone complains that they are getting exactly the same error.

 

Here I was thinking that IID '{70109AA1-63C1-11D2-B78B-0060B0EC020B}' or 0x80004002 might give some indication as to where the problem was occurring in my code - like somewhere in there was a line number or something - but apparently it means something else?

 

0 Likes
Message 18 of 33

Anonymous
Not applicable

Here is a vba sub for to create the views.

 

Public Sub CreateViewReps()
    ' Set a reference to the assembly component definintion.
    ' This assumes an assembly document is open.
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
    
    Dim oAssyDoc As AssemblyDocument
    Set oAssyDoc = ThisApplication.ActiveDocument

    Dim oRepManager As RepresentationsManager
    Set oRepManager = oAsmCompDef.RepresentationsManager

    Dim docFile As PartDocument
    
    Dim LastActiveView As String
    Let LastActiveView = oRepManager.ActiveDesignViewRepresentation.Name
    
    For Each docFile In oAssyDoc.AllReferencedDocuments
        If docFile.DocumentType = 12290 Then
            Dim sName As String
            Let sName = Left(docFile.DisplayName, Len(docFile.DisplayName) - 4)
            
            Dim oDesView As DesignViewRepresentation
            Dim foundName As Boolean
            Let foundName = False
            
            For Each oDesView In oRepManager.DesignViewRepresentations
                If oDesView.Name = sName Then
                    foundName = True
                End If
            Next
            
            If foundName = False Then
                oRepManager.DesignViewRepresentations.Add (sName)
                Dim oCompOcc As ComponentOccurrence
                
                For Each oCompOcc In oAsmCompDef.Occurrences
                    If oCompOcc.Name = sName & ":1" Then
                        oCompOcc.Visible = True
                    Else
                        oCompOcc.Visible = False
                    End If
                Next
            End If
        End If
    Next
    
    
    oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item(LastActiveView).Activate

End Sub

and here is the sub to create the drawing views. First create a drawing and place one view of the main assembly.

 

Public Sub CreateViewForEachRep()
    ' Set a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
    
    'Set a reference to the active sheet.
    Dim oSheet As sheet
    Set oSheet = oDrawDoc.ActiveSheet
    
    Dim viewScale As Double
    Let viwScale = 0.1875
    
    Dim ptx As Long
    Let ptx = 0
    
    
    'this assumes that view1 is of the full assembly
    Dim assyDocName As String
    Let assyDocName = oSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.FullDocumentName
        
    Dim assyDoc As AssemblyDocument
    'open assembly doc invisibly
    Set assyDoc = ThisApplication.Documents.Open(assyDocName, False)
        
    Dim oAssyCompDef As AssemblyComponentDefinition
    Set oAssyCompDef = assyDoc.ComponentDefinition
    
    Dim oRepMgr As RepresentationsManager
    Set oRepMgr = oAssyCompDef.RepresentationsManager
    
    Dim oDesView As DesignViewRepresentation
    
    For Each oDesView In oRepMgr.DesignViewRepresentations
        If oDesView.Name <> "Default" And oDesView.Name <> "Master" Then
            
            Dim oPoint1 As Point2d
            Set oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(ptx, 11)
            
            Call PlaceBaseView(oSheet, assyDoc, oPoint1, oDesView.Name)
                        
        End If
        ptx = ptx + 14
    Next
      
      'close assembly doc
      assyDoc.Close (False)
      
End Sub

Public Sub PlaceBaseView(ByRef sheet As Inventor.sheet, _
                        ByRef assyDoc As AssemblyDocument, _
                        ByRef ctrpoint As Point2d, _
                        ByRef viewName As String)

    'define view orientation
    Dim vieworient1 As ViewOrientationTypeEnum
    Let vieworient1 = ViewOrientationTypeEnum.kFrontViewOrientation

    'define view style
    Dim viewstyle1 As DrawingViewStyleEnum
    Let viewstyle1 = DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle
    
    Dim oFaceView As DrawingView
    Set oFaceView = sheet.DrawingViews.AddBaseView(assyDoc, ctrpoint, 0.5, vieworient1, viewstyle1, viewName)

End Sub

 

You will need to modify the view placement and scales 

Message 19 of 33

dan_inv09
Advisor
Advisor

Sorry, I ran out of time and had to brute force it.

 

When this job is done and I have some time I will play around with a dummy assembly (or the next job) and see how it works.

 

Thanks for the effort.

 

 

0 Likes
Message 20 of 33

dan_inv09
Advisor
Advisor
For Each docFile In oAssyDoc.AllReferencedDocuments

gives me runtime error "13": type mismatch 

0 Likes