API: Inventor Drawing sketch object vs. ObjectDBX

mikazakov
Advocate
Advocate

API: Inventor Drawing sketch object vs. ObjectDBX

mikazakov
Advocate
Advocate

Hi,

I tested Inventor API vs. ObjectDBX. The ObjectDBX 10 times faster.
I understand that these lines of different objects.
When measure by use CPU ticks, and DeferUpdate=True, I see that Inventor API increases productivity by 30%
But 30% is not "much faster". ObjectDBX really "much faster".Smiley Surprised
Is there a the Inventor API capacity for productivity? Or its limit?

Do selected the entity of ObjectDBX impossible?

I attached sample for external VB.NET:

 

    Sub Main()
        Dim InvApp As Application = GetInventorApplication()

        Dim sw As Stopwatch = New Stopwatch


        sw.Start()
        TestInventorLines(InvApp)
        sw.Stop()
        Dim TicsInv As Int64 = sw.ElapsedTicks

        Console.WriteLine("Inventor API Ticks: {0}", TicsInv)
        sw.Reset()

        sw.Start()
        TestAcadLines(InvApp)
        sw.Stop()

        Dim TicsAcad As Int64 = sw.ElapsedTicks

        Console.WriteLine("Acad ObjectDBX Ticks: {0}", TicsAcad)

        Console.WriteLine("{0} times the difference", TicsInv / TicsAcad)

        Console.ReadKey()
    End Sub


    Sub TestInventorLines(ThisApplication As Application)

        Dim tg As TransientGeometry : tg = ThisApplication.TransientGeometry
        Dim draw As DrawingDocument : draw = ThisApplication.ActiveDocument
        Dim s As DrawingSketch : s = draw.ActiveSheet.Sketches.Add()

        ThisApplication.ScreenUpdating = Not ThisApplication.ScreenUpdating

        s.Edit()
        s.DeferUpdates = True

        For i = 0 To 500
            Dim p1 As Point2d : p1 = tg.CreatePoint2d(0, i * 0.05)
            Dim p2 As Point2d : p2 = tg.CreatePoint2d(10, i * 0.05)

            Call s.SketchLines.AddByTwoPoints(p1, p2)
        Next
        s.ExitEdit()

        s.DeferUpdates = False
        ThisApplication.ScreenUpdating = True
        draw.Update()
    End Sub

    Sub TestAcadLines(ThisApplication As Application)
        Dim draw As DrawingDocument : draw = ThisApplication.ActiveDocument
        Dim ad As AXDBLib.AcadDatabase : ad = draw.ContainingDWGDocument

        For i = 0 To 500
            Dim startPoint(0 To 2) As Double
            Dim endPoint(0 To 2) As Double

            ' Define the start and end points for the line
            startPoint(0) = 10 : startPoint(1) = i * 0.05 : startPoint(2) = 0
            endPoint(0) = 15 : endPoint(1) = i * 0.05 : endPoint(2) = 0

            Call ad.PaperSpace.AddLine(startPoint, endPoint)
        Next

        draw.Update()
    End Sub


    'Функция получения ссылки на Inventor
    Function GetInventorApplication() As Application
        Dim InvApp As Application
        Try
            'Попытка присоединится к загруженному в память Inventor
            InvApp = CType(Marshal.GetActiveObject("Inventor.Application"), Application)
        Catch ex As Exception
            'Загрузка Inventor в память и присоединение к нему
            Dim InvType As Type = Type.GetTypeFromProgID("Inventor.Application")
            InvApp = CType(Activator.CreateInstance(InvType), Application)
            InvApp.Visible = True
        End Try
        Return InvApp
    End Function
0 Likes
Reply
Accepted solutions (1)
624 Views
2 Replies
Replies (2)

ekinsb
Alumni
Alumni
Accepted solution

There are some basic differences in how AutoCAD and Inventor work, and especially in this context.  In your Inventor code that creates the lines there's quite a bit more going on than in the AutoCAD code.  This could be seen as a negative of the Inventor API and I'm not commenting either way on that but just saying that there is a bit more going on that just creating the lines so it’s not really a direct comparison.  For example you're creating a new sketch, activating the sketch for edit, creating two transient points for each line, creating a line, exiting the edit, and updating the document. The only AutoCAD API call you're making is the AddLine call.

 

AutoCAD provides a lower level API to create the data more directly in the database, whereas with Inventor you're essentially reproducing the same workflow as you see in the user interface.  These additional steps you have to take in Inventor are responsible for some of the additional time but the majority of the time is being spent in the AddByTwoPoints call.  You'll also notice a big difference in the results if you do an undo after running your program.  The lines created using AutoCAD are undone in a single step (actually it when the undo of the last Inventor line is undone) while the Inventor lines are undone one at a time.

 

The creation of "transactions" is a fairly expensive operation in Inventor and each call to AddByTwoPoints is creating a new transaction, which allows it to be undone as a separate undo operation.  Often you want to group operations into a single undo, which you can do through the API by using calling the TransactionManager.startTransaction, doing your work, and then calling the End method on the transaction.  This creates a new transaction that wraps all of the inner transactions so they are undone in a single undo.  However, this doesn't help with performance because each call to AddByTwoPoints is still wrapped in its own transaction.

 

There is another, undocumented, method of starting a transaction called startGlobalTransaction.  This is used the same as the standard startTransaction but in this case, all of the calls between start and End don't have their own transaction.  When I wrapped your Inventor sketch creation in a global transaction the time difference went down to the Inventor creation being 5.28 times the difference, whereas without that I was seeing 17.72 times slower.  I then removed the call to Update, which really isn't needed, and it takes it down to 4.12 times slower.

 

The reason startGlobalTransaction is undocumented is because of the potential dangers in using it incorrectly.  If you call startGlobalTransaction and then perform whatever operations you're doing and it fails at some point and you don't do anything to clean up the transaction (abort or end) it leaves Inventor in a bad state.  So as long as you handle errors correctly and abort or end the global transaction there shouldn't be any issues with using it and as you can see in cases where you're making a lot of calls that are transacted it can make a big difference in the performance of your programs.

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog

mikazakov
Advocate
Advocate

Thank you, Brain for you detailed answer.

startGlobalTransaction much faster really.

startGlobalTransaction will help faster draw my tables in the drawings.

0 Likes