Robustly loop through sheets to generate DWF files...

Robustly loop through sheets to generate DWF files...

russ.green
Advocate Advocate
657 Views
7 Replies
Message 1 of 8

Robustly loop through sheets to generate DWF files...

russ.green
Advocate
Advocate

I've got an addin under development that assists with the drawing issue process.  Essentially I load the sheets in the model to a listview where sheets can be selected for issue.  Then the addin loops through each and performs some actions basad on options selected by the user.

 

So I want to process each sheet in turn as I output files (DWG or DWF) to an issue folder and want the filenames to be written in a format that also included the revision letter.

 

So I select all the sheets in the active document then loop through the listivews selected items. for each listviewitem i loop through the sheets collected and check if the drawing numbers match.  This process seems to work just fine when debugging.

 

Dim sheets As New FilteredElementCollector(doc)
sheets.OfClass(GetType(ViewSheet))

For Each oItem As ListViewItem In Me.lvwSheets.CheckedItems
   For Each sheet As ViewSheet In sheets
      If oItem.Text = sheet.SheetNumber Then
         Dim thisdoc As Document = uidoc.Document
         Dim views As ViewSet = New ViewSet
         views.Insert(sheet)

         Dim sbFile As New System.Text.StringBuilder
         sbFile.Append(doc.ProjectInformation.Number & " - ")
         sbFile.Append(sheet.SheetNumber & "_")
         sbFile.Append(sheet.Name & "_")
         sbFile.Append("rev" & sheet.Parameter(BuiltInParameter.SHEET_CURRENT_REVISION).AsString)

         trans = New Autodesk.Revit.DB.Transaction(doc, "Export Sheets")
         Using trans
              trans.Start()
              If Me.chkExportDWG.Checked = True Then ExportDWG(sbFile.ToString & ".dwg", views)
               If Me.chkExportDWF.Checked = True Then ExportDWF(sbFile.ToString & ".dwf", GetSheetsize(sheet, doc), thisdoc, views)
               If Me.chkPrint.Checked = True Then PrintSheet(sheet, thisdoc)
               trans.Commit()
          End Using
pb1.Value = pb1.Value + 1 pb1.Refresh() End If Next Next


What I've dound is that on live jobs the addin occasionally crashes.  I don't think its the code above as it never crashes if I don't select any export formats.  The addin simply records the drawings in the drawing issue database and completes neately.  I occosionally (and seemingly randomly) get crashes with the export file. An example being how I'm generated a DWF for the given sheet.

 

    Private Sub ExportDWF(ByVal ExportFileName As String, ByRef sheetsize As ExportPaperFormat, ByRef thisdoc As Document, ByRef views As ViewSet)
        Try
            ptManager = thisdoc.PrintManager
            Dim ptSetup As PrintSetup = ptManager.PrintSetup
            Dim ptParameters As PrintParameters = ptSetup.CurrentPrintSetting.PrintParameters

            If ptParameters.Zoom <> 100 Then
                ptParameters.ZoomType = ZoomType.Zoom
                ptParameters.Zoom = 100
            End If

            Try
                ptManager.ViewSheetSetting.SaveAs("eproject")
            Catch ex As Exception
            End Try
            Try
                ptManager.PrintSetup.SaveAs("eproject")
            Catch ex As Exception
            End Try

            Dim DWFExpOpt As DWFExportOptions
            DWFExpOpt = New DWFExportOptions
            With DWFExpOpt
                .ImageFormat = DWFImageFormat.Lossless

                If Me.rdoDWFDefault.Checked = True Then .ImageQuality = DWFImageQuality.Default
                If Me.rdoDWFLow.Checked = True Then .ImageQuality = DWFImageQuality.Low
                If Me.rdoDWFMedium.Checked = True Then .ImageQuality = DWFImageQuality.Medium
                If Me.rdoDWFHigh.Checked = True Then .ImageQuality = DWFImageQuality.High

                .ExportingAreas = chkDWFExportAreas.Checked
                .ExportObjectData = chkDWFObjectData.Checked
                .PaperFormat = sheetsize

            End With

            doc.Export(sbIssuePath.ToString, ExportFileName, views, DWFExpOpt)

            Try
                ptManager.ViewSheetSetting.Delete()
            Catch ex As Exception
            End Try
            Try
                ptManager.PrintSetup.Delete()
            Catch ex As Exception
            End Try

        Catch ex As Exception
            TaskDialog.Show("There has been an error processing sheet export to DWF for " & ExportFileName & Environment.NewLine & ex.ToString, MsgBoxStyle.Information, "Error processing sheets")
        End Try
    End Sub

 The crash never occurs when I step through on the debugger so I'm wondering if its connected to the speed the addin is running and the sheet not completing a refresh on the screen????

 

Any ideas would be most welcome...

 

 

Russ Green
0 Likes
658 Views
7 Replies
Replies (7)
Message 2 of 8

Revitalizer
Advisor
Advisor

Hi russ.green,

 

what about using Log4Net for getting more information about what's going on when your add-in is running ?

You may log whatever you want and look at the written log file to find out what's going wrong.

 

Generally, when an addin is running on a customer's computer, you cannot debug it, so you will need an error logging tool *at all*.

 

Best regards,

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 3 of 8

arnostlobel
Alumni
Alumni

Hello Russ,

 

If I did not know about anything else about the problems you experience except for the symptoms, my first guess would be it is caused by garbage collecting. Often, when object deletion synchronizations involved, it would be possible to see crashes when running the application normally, but not when debugging it. Unfortunately, it is not easy to track it down. For once your code is still quite complex to use it for GC problem hunting. Also, I am not absolutely convinced it is GC-related, so it is probably wise to start with some changes to your code and see what happens (if anything). You wanted some ideas, so here they are: 

1. I think you use the 'Using' block incorrectly. I am not a VB programmer, but I suspect it is going to be similar to C#, and if so, you are supposed to define the object 'inside' your 'Using' block, not outside. Therefor it will be

Using Transaction trans = new Transaction(doc, "Export Sheets")

2. I believe you want to RollBack each transaction, not Commit it, as committing makes little sense if all you are interested in export of views. (I do not think this attributes to the problems, but I wanted to mention it anyway)

3. I recommend getting rid of, at least temporarily, all the code that deals with Print Manager to make sure that no accidental mistakes there would affect the export. At the very least do not attempt to delete the current sheet settings and print setup. Simply try to do the raw export with just the option class set up and applied.

4. I see you have a lot blank Try-Catch blocks, thus you might not see a problem when it actually occurs. In cases like this (in fact, you should always put something there), have at least some debug message in each and every try-catch.

 

Arnošt Löbel

Autodesk Revit R&D

Arnošt Löbel
0 Likes
Message 4 of 8

russ.green
Advocate
Advocate

I've installed VS on the machine I'm haveint trouble with and stepped through the debugger....same revit model but its falling over here:

 

ptManager = thisdoc.PrintManager
Dim ptSetup As PrintSetup = ptManager.PrintSetup
Dim ptParameters As PrintParameters = ptSetup.CurrentPrintSetting.PrintParameters

If ptParameters.Zoom <> 100 Then
    ptParameters.ZoomType = ZoomType.Zoom
    ptParameters.Zoom = 100
End If

 with this exception:

 

Current Zoom Type is NOT Zoom.

 

So how are you suppoaws to Export to DWF.  When I don't try and configure some print settings first I find the DWF is not printed at 100% zoom and scales to fit on the sheet.  Most unhelpful!

Russ Green
0 Likes
Message 5 of 8

arnostlobel
Alumni
Alumni

Hello Russ,

 

I am glad you made progress in discovering the possible cause of the crashes you experienced. I kind of suspected it was likely related to the print setup.

 

Now, I am no expert in that part of Revit API, so please take my following advice with a grain of salt. (I was only interested in the mysterious crashes. Once it is not so mysterious, I am not that interested 😉

 

My assumption is as follows: You did not mention from where the exception is thrown. From your text I take it you assume it is thrown from the line where you attempt to set the Zoom to 100, but my guess would be that it is thrown from the line where you test if the zoom is not equal to 100. My reasoning is that if the Zoom Type is not Zoom, you are probably not permitted to even ask for zoom level, as it does not make sense for the other types.

 

Can you confirm from what line of code is the exception thrown?

 

Arnošt Löbel

Autodesk Revit R&D

Arnošt Löbel
0 Likes
Message 6 of 8

russ.green
Advocate
Advocate

it falls over on the if line...

Russ Green
0 Likes
Message 7 of 8

arnostlobel
Alumni
Alumni

So there you go :-). I would assume you have to test first if the Zoom Type is Zoom and set ot to Zoom if it is not. After that you should be able to set the desirable zoom level. I have not tried it myself, but I suggest you give it a try.

 

Arnošt Löbel

Autodesk Revit R&D

Arnošt Löbel
0 Likes
Message 8 of 8

Revitalizer
Advisor
Advisor

Dear russ.green,

 

it says in the RevitAPI.chm, in the ImageExportOptions chapter:

"Zoom: The value for Zoom (as a percentage). Used only when ZoomType is Zoom."

So I'm with Arnost: first test the ZoomType, then get or set the Zoom value.

 

 

Cheers,

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes