Programming technique, to speed up loops?

Programming technique, to speed up loops?

omartin
Advocate Advocate
538 Views
4 Replies
Message 1 of 5

Programming technique, to speed up loops?

omartin
Advocate
Advocate

So I have a simple loop to remove the overrides from a partslist:

 

ThisApplication.ScreenUpdating = False

For Each plr In pl.PartsListRows
    plr("col1").Static = False
    plr("col2").Static = False
   
    If plr("col1") = "" Then
    plr("col1").Value = "No"
    End If    
Next plr

ThisApplication.ScreenUpdating = true

 

as expected on small assemblies there is no issues, on larger ones it is not a huge delay but noticeable as well as mouse flickering.

 

question is when editing the parts list manually, and then hitting ok, the changes seem to be almost instant after hitting ok.

My guess is when iterating through the steps, it is like hitting the "ok" button at the end of each loop.

 

Is there a more efficient way to perform such a loop, where all the changes are done and then applied in "one shot" (for a lack of better description) like modifying the table manually and then hitting the ok button?

Was my reply Helpful ? send a Kudos or accept as solution
0 Likes
Accepted solutions (1)
539 Views
4 Replies
Replies (4)
Message 2 of 5

YuhanZhang
Autodesk
Autodesk

Can you try below code if it helps?

 

ThisApplication.ScreenUpdating = False

Dim oTransaction As Transaction
Set oTransaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Edit Partslist")

For Each plr In pl.PartsListRows
    plr("col1").Static = False
    plr("col2").Static = False
   
    If plr("col1") = "" Then
    plr("col1").Value = "No"
    End If
Next plr

oTransaction.End
ThisApplication.ScreenUpdating = True

 

It wrap your multiple edit to the partslist in a custom transaction.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 3 of 5

omartin
Advocate
Advocate

Thanks for the reply Rocky, It didn't speed it up any, however; the transaction is really helpful and I will start using.

 

I was trying to create a datatable to mimic what i want to change but it wasn't working out...

 

If there was another built in object that represents the parts list as a flat table, or some way to access the PL as a datatable or something where i could just update text would be good, maybe I will add that to the feature request.

Was my reply Helpful ? send a Kudos or accept as solution
0 Likes
Message 4 of 5

YuhanZhang
Autodesk
Autodesk
Accepted solution

How about if set the DeferUpdates:

ThisApplication.ScreenUpdating = False

Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim bDeferUpdate As Boolean

bDeferUpdate = oDoc.DrawingSettings.DeferUpdates
oDoc.DrawingSettings.DeferUpdates = True

Dim oTransaction As Transaction
Set oTransaction = ThisApplication.TransactionManager.StartTransaction(oDoc, "Edit Partslist")

For Each plr In pl.PartsListRows
    plr("col1").Static = False
    plr("col2").Static = False
   
    If plr("col1") = "" Then
    plr("col1").Value = "No"
    End If
Next plr

oTransaction.End
oDoc.DrawingSettings.DeferUpdates = bDeferUpdate
ThisApplication.ScreenUpdating = True


If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 5 of 5

omartin
Advocate
Advocate

That did speed it up a bit (~5 secs from my hand stop watch) not as instantaneous as editing the table but I guess it is the closest we can get.

It would be good to know how the partslist works (pragmatically) encase I wanted to make a custom partslist editor...

 

Thanks Rocky

Was my reply Helpful ? send a Kudos or accept as solution
0 Likes