- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've created a catalog assembly where we house all of our parts and subassemblies from our custom library. I have also made a macro call "PlaceFromCatalog" that opens up the assembly, allows you to select the parts you want, closes the catalog, and allows you to place the part in the other assembly. I am wondering if there is a way to hide the catalog without closing it. I would like to do it this way because every time you close the catalog it resets the undo feature and I want to be able to undo the placement of the parts in a different assembly. I also want to do it this way so that it opens quicker. I'm already opening the catalog with "All Components Suppressed" but I still want it to open faster. If I close the catalog assembly within the "PlaceFromCatalog" command transaction, sometimes it clears the undo. According to the help, it says I can close the document within the same transaction and it will work but if I activate another command and then end the transaction it will crash.
As per "Supporting Undo" in the "Inventor API User's Manual":
Dos and Don'ts
- An Undo or Redo should be performed only when there is no transaction in progress (during an unidentified transaction).
- Do not wrap any Inventor commands within transactions. Do not wrap any user interaction events within transactions. A good rule would be that it is OK to create modal dialogs within a transaction but not modeless dialogs.
- The transaction event handling code should not perform any operation that uses transactable memory. For example, it is illegal to create a sketch line in the event handling code for the commit of a transaction.
- Do not close any document inside a transaction, even the active one. The exception to this rule is that it is legal to close a document in a transaction when the document was opened in that exact same transaction.
Dim oTxnMgr As TransactionManager
Set oTxnMgr = ThisApplication.TransactionManager
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
Dim oTxn As Transaction
Set oTxn = oTxnMgr.StartTransaction(oDoc, "Txn1")
' ***************************************
' Invalid operation
' ***************************************
oDoc.Close
oTxn.End
I just figured the easiest way to pull parts from the catalog is to already have it open and just hide it after use since we use it quite a lot and it will speed things up. I just don't know how to do it. Here is my code for reference:
Quick question: how do I add a spoiler?
Sub PlaceFromCatalog()
' Make sure you are in an assembly environment
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then Exit Sub
' Stops a different macro that causes interference with this one
Set oiMateEditor = Nothing
' Starts the transaction to allow undo
Dim oTxn As Transaction
Set oTxn = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "InsertFromCatalog")
' Calls a function that opens the catalog and returns the document
Set oCatalog = openCatalog
' Creates a class that monitors UserInputEvents and DocumentEvents to see
' if a component is selected or if the catalog is closed
Set oSelect = New SelectClass
' Waiting for something to happen
Do While oSelect.oLocation = ""
DoEvents
Loop
' If a component is selected, the location of the file in the library is returned...
oLocation = oSelect.oLocation
Set oSelect = Nothing
' If the document is closed by the user, the string "canceled" is returned
' and cleanup begins to stop the command
If oLocation = "Cancel" Then
' Ending the transaction has caused me an error before which is why I
' tell the code to ignore errors
On Error Resume Next
oTxn.End
On Error GoTo 0
' This starts the other another macro that I ended which needs to be running
Set oiMateEditor = New ReUsableiMateHandler
Exit Sub
' If multiple components are selected, they are copied into memory and the
' SelectClass returns "Copied"
ElseIf oLocation = "Copied" Then
oCatalog.Close (True)
On Error Resume Next
oTxn.End
On Error GoTo 0
Set oCatalog = Nothing
Set oiMateEditor = New ReUsableiMateHandler
Exit Sub
End If
' A component has been selected
oCatalog.Close (True)
On Error Resume Next
' This transaction captures the opening and closing of the catalog
oTxn.End
On Error GoTo 0
' Start a new transaction that captures placing the components
Set oTxn2 = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Place From Catalog")
Dim cmdMgr As CommandManager
Set cmdMgr = ThisApplication.CommandManager
ThisApplication.AssemblyOptions.UseiMate = False
' Post the file Location so that way the dialog for the Place Command doesn't open
Call cmdMgr.ClearPrivateEvents
Call cmdMgr.PostPrivateEvent(kFileNameEvent, oLocation)
' Execute the "Place Component" command synchronously and wait for it to finish
Call cmdMgr.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute2(True)
oTxn2.End
Set oiMateEditor = New ReUsableiMateHandler
End Sub
Solved! Go to Solution.