Problem with Transaction in Inventor 2018 library's or cc's file

Problem with Transaction in Inventor 2018 library's or cc's file

danipon71
Enthusiast Enthusiast
1,229 Views
9 Replies
Message 1 of 10

Problem with Transaction in Inventor 2018 library's or cc's file

danipon71
Enthusiast
Enthusiast

I'm experiencing some problem working with Transaction in Inventor 2018 with library's or content center's file.

I attacched a simple addin to reproduce the problem.

I create a sample button that add a property in every document referenced by the main assembly.

The activity is wrapped by a transaction.
Assuming to work with an assembly with two parts: If one of the referenced docs is a library's or content center's component an exception was thrown (and that is sadly known) but this exception cause an unexpected corruption of my transaction that will throw an exception calling the End method on it.

 

It seems like this exception cause my transaction to be aborted, this would explain why if the first document is the cc file I find the property created properly and the action not named "Sample Transaction":
createProp.PNG

 

 

 

 

 

 

 

 

 

and I don't see anything if the first document is not the cc file:

propNotCreated.PNG



 

 

 

 

If I'am not working with lib or cc file everything goes well:

properly.PNG

 

 

 

 

 

 

 

 

 

Here some code's lines:

 

        Trans = invAppl.TransactionManager.StartGlobalTransaction(oAsmDoc, "SampleTransaction")
        Try
            For Each oOcc As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
                CreateProperty(oOcc.Definition.Document, "myProp", "myPropValue")
            Next
        Catch ex As Exception
            Trans.Abort()
        End Try
        Trans.End()

    Private Function CreateProperty(doc As Document, ByVal propName As String, ByVal propValue As Object) As Inventor.Property
        Dim prop As Inventor.Property = Nothing
        Dim pset As PropertySet
        Try
            ' Get user defined property set
            pset = doc.PropertySets.Item("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
            prop = pset.Add(propValue, propName)
        Catch ex As Exception                 
            MsgBox(ex.ToString())
        End Try
        Return prop
    End Function



Yes I could check if I have a lib or CC document but it's not as simple as it seems in this sample.

 

0 Likes
1,230 Views
9 Replies
Replies (9)
Message 2 of 10

MechMachineMan
Advisor
Advisor
        Trans = invAppl.TransactionManager.StartGlobalTransaction(oAsmDoc, "SampleTransaction")
        Try
            For Each oOcc As ComponentOccurrence In oAsmDoc.ComponentDefinition.Occurrences
                CreateProperty(oOcc.Definition.Document, "myProp", "myPropValue")
            Next
        Catch ex As Exception
            Trans.Abort()
        End Try
        Trans.End()

    Private Function CreateProperty(doc As Document, ByVal propName As String, ByVal propValue As Object) As Inventor.Property
        Dim prop As Inventor.Property = Nothing
        Dim pset As PropertySet
        
If doc.isModifiable = True pset = doc.PropertySets.Item("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}") prop = pset.Add(propValue, propName) Else MsgBox(ex.ToString()) End If Return prop End Function

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 10

danipon71
Enthusiast
Enthusiast

@danipon71wrote:
Yes I could check if I have a lib or CC document but it's not as simple as it seems in this sample.

I don't want to use your workaround, because in my code it's not only a matter of creating property but also setting property and I don't know what else: If I manage properly transaction and exception handling I expect that everything goes well!

If CreateProperty method throw a .net exception or other COM exceptions, the transaction does its work and everything is ok.

    Private Function CreateProperty(doc As Document, ByVal propName As String, ByVal propValue As Object) As Inventor.Property
        Dim prop As Inventor.Property = Nothing
        Dim pset As PropertySet
        Try
            ' Get user defined property set
            pset = doc.PropertySets.Item("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
            ' Throwing a COM Exception doesn't corrupt my transaction  
prop = pset.Item(1000) Catch ex As Exception MsgBox(ex.ToString()) End Try Return prop End Function

 

0 Likes
Message 4 of 10

danipon71
Enthusiast
Enthusiast

I just found out that using StartTransaction instead of StartGlobalTransaction doesn't throw any exception on my transaction.


According to this post the global transaction improves performance and for my command perfomance matters.

Is this a bug or am I missing something about global transaction?

 

 

0 Likes
Message 5 of 10

MechMachineMan
Advisor
Advisor

I dont understand how you can call my solution a "workaround".

 

A workaround is saying "we know this happens, we don't know why, but this works to fix it!"

 

What my solution is saying is "we know this happens, we DO know why, and this is how we perform the proper test to prevent it from happening".

 

 

Regardless of what you are trying to do, mucking with a file which .IsModifiable returns false will ALWAYS cause issues, so a proper solution is using THAT TEST to ensure that THIS SCENARIO is properly handled; instead of throwing in a generic error handler that doesn't pin-point the cause nor apply an appropriate solution and will also HIDE any other potential unexpected errors.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 6 of 10

danipon71
Enthusiast
Enthusiast

Ok, you're right: let's not call your solution a workaround. IsModifiable is the proper way to avoid exception creating property on CC file.


"we know this happens, we DO know why, and this is how we perform the proper test to prevent it from happening".

Now, let's talk about an another problem: transaction wrapping PropertySet.Add; according to documentation I'm assuming that PropertySet.Add do not cause the active transaction to be aborted, the method could throw an exception (attempting to add an already existing prop, attempting to create a property on CC file, ...) but exception is exception not an undefined behaviour. This is the contract, am I wrong?
So I'm reporting that calling PropertySet.Add on CC file cause my wrapping transaction to be aborted.

Do you and Autodesk know about this?
No --> bug.
Yes --> Do you and Autodesk know why this happens?
  No --> bug, but there's a workaround: use IsModifiable (not so simple in my code) OR use StartTransaction instead of StartGlobalTransaction (simple, but performance is worse)
  Yes --> And you (me) are wrong assuming this, this and this OR StartGlobalTransaction is now deprecated OR I don't know what else.

 

 

0 Likes
Message 7 of 10

MechMachineMan
Advisor
Advisor

Error loops = "lazy man's way" = "more efficient" = "workaround"

 

Something similar to the code below should work in all cases without causing an error.

 

I do agree with you that it seems weird that the transaction automatically aborts itself when it encounters an exception - EVEN THOUGH THAT EXCEPTION IS HANDLED IN CODE - but it also seems like it must be tough for the program to differentiate; so they coded it painfully this way so that users don't cause the application to crash constantly by poorly coding the handling of the transaction.

 

The only thing I could think to advise going forward is to handle the situation as below; code anything wrapped within the transaction to never throw exceptions. (OR the abort process could just be related to certain operations??? ie; PropertySet changes?, and maybe a simple query on the propertyset/assigning it to a variable to check it's existence would not cause the issues)

 

 

Sub Main()
    MsgBox (CreateProperty(ThisApplication.ActiveDocument, "Material1", "").Value)
End Sub


    Private Function CreateProperty(doc As Document, ByVal propname As String, ByVal propValue As String) As Inventor.Property
        Dim prop As Inventor.Property
        Set prop = Nothing

        Dim apset As Inventor.PropertySet
        Set apset = doc.PropertySets.Item("Inventor User Defined Properties")
        
        If PropExists(doc, apset, propname) = True Then
            MsgBox ("PropExists!")
        Else
            If doc.IsModifiable = True Then
                MsgBox ("Prop Does Not Exist BUT it IS Modifiable!")
                Set prop = apset.Add(propValue, propname)
            Else
                MsgBox ("Prop Does Not Exist AND the doc is Locked!")
                prop = Nothing
            End If
        End If
        
        Set CreateProperty = prop
    End Function
    
Function PropExists(doc As Document, propset As Inventor.PropertySet, propname As String) As Boolean
    Dim oProp As Variant
    Dim oPropIDS() As Long
    Dim oPropNames() As String
    Dim oPropVals() As Variant
    
    Call propset.GetPropertyInfo(oPropIDS, oPropNames, oPropVals)
    
    For Each oProp In oPropNames
        If oProp = propname Then
            PropExists = True
            Exit Function
        End If
    Next
    PropExists = False
End Function

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 8 of 10

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @danipon71,

 

I downloaded attachment and trying to run the code using Visual Studio 2015 and Inventor 2018.

 

Unfortunately, unable to trigger breakpoints inside source code. I didn't find any code related to transaction.

 

Any special procedure to run the source code? If it is there, please specify.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 9 of 10

danipon71
Enthusiast
Enthusiast

Hi @chandra.shekar.g,

I create another addin in VS 2015 and make it simpler.

  1. Build the project
  2. Copy the .dll, .pdb and .addin in the proper location
  3. Create an assembly with one standard part and one CC's file
  4. Attach to Inventor process
  5. Set breakpoint in StandardAddInServer.vb at line 128: prop = pset.Add(propValue, propName)
  6. Go to the tools tab and click on the Test transaction command
  7. The property creation on standard part is ok, the property creation on CC's file abort the transaction before I could catch the expected exception
  8. Check the Inventor's Undo user interface

Try to swap the order of the two files (closing and re-opening the assembly) and check the difference in the Inventor's Undo UI

Try to run two times in a row the command and see the exception thrown by adding  an already existing property to the standard part do NOT cause the abort of my transaction.
Try to use StartTransaction instead of StartGlobalTransaction and see my transaction not beeing aborted.

Let me know if you can reproduce the issue.

Thank you.

0 Likes
Message 10 of 10

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @danipon71,

 

Attached source code is able to execute successfully.

 

Transaction using StartGlobalTransaction causes abort when trying to add custom property to content center file. But, Transaction using StartTransaction do not cause any abort and transaction completes successfully.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes