Code runs from assembly, saves all related drawings to hi res .png.. but...

Code runs from assembly, saves all related drawings to hi res .png.. but...

Anonymous
Not applicable
744 Views
6 Replies
Message 1 of 7

Code runs from assembly, saves all related drawings to hi res .png.. but...

Anonymous
Not applicable

The ThisApplication.Activeview.Fit line does not work on the first opened drawing.  All of the others it corrects.

I've seen this issue around before, maybe there's some random thing I can run before fitting the view so that it snaps back and forth and gets it working.

 

 

 

 

Function fileExists(fname) As Boolean
    If Dir(fname) <> "" Then fileExists = True Else fileExists = False
End Function



Public Sub IDWtoPNG()

    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument
    If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
        MsgBox "This code prints our drawings for assembly documents only.", vbExclamation
        Exit Sub
    End If

    If MsgBox("Did you hit print to PNG by accident?", vbYesNo + vbQuestion) = vbYes Then
        Exit Sub
    End If

    dirPath = Left(oAsmDoc.fullfilename, Len(oAsmDoc.fullfilename) - Len(oAsmDoc.DisplayName))
    Dim oRefDocs As DocumentsEnumerator
    Set oRefDocs = oAsmDoc.AllReferencedDocuments
    Dim oRefDoc As Document
    'numFiles = 0
    
      Dim temp As String
            Dim temper As String
            Dim basefilename As String
            Dim fullfilename As String
            Dim prefix As String
            Dim prefixer As String
            

    For Each oRefDoc In oRefDocs
        idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"
        If fileExists(idwPathName) Then
            'numFiles = numFiles + 1
            Dim oDrawDoc As DrawingDocument
            Set oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
            oDrawDoc.Activate
            Set oDrawDoc = ThisApplication.ActiveDocument
    
    'Get the active view.
            Dim oView As View
            Set oView = ThisApplication.ActiveView
            ThisApplication.ActiveView.Fit
                                             
            
    ' Save the view as a jpg file.
            prefix = oAsmDoc.FullDocumentName
            temper = Right$(prefix, Len(prefix) - InStrRev(prefix, "\"))
            prefixer = Left(temper, Len(temper) - 4)
            
            fullfilename = idwPathName
            temp = Right$(fullfilename, Len(fullfilename) - InStrRev(fullfilename, "\"))
            
            basefilename = Left$(temp, InStrRev(temp, ".") - 1)
            oPngFileName = "C:\IDW's\" & prefixer & basefilename & ".png"
            Call oView.SaveAsBitmap(oPngFileName, 3000, 2320)
            oDrawDoc.Close (True)
        End If
    Next


    MsgBox "It is complete!  It is complete."
            End Sub

 

0 Likes
Accepted solutions (2)
745 Views
6 Replies
Replies (6)
Message 2 of 7

MechMachineMan
Advisor
Advisor
Accepted solution

Couple of things to maybe experiment with:

1. Add a wait loop to make sure ThisApplication.Ready returns true before proceeding.

2. Use the command/control definition to fit the view.

3. Play around with assignment and use of the view variable; ie Set oView = ThisApplication.ActiveView: oView.Fit


--------------------------------------
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
Message 3 of 7

MechMachineMan
Advisor
Advisor
Accepted solution

There is also this blog post from a while back that may help out:

 

http://adndevblog.typepad.com/manufacturing/2012/08/use-a-camera-object-to-set-the-view-of-a-documen...


--------------------------------------
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
Message 4 of 7

Anonymous
Not applicable

The camera stuff works, but it's slow, so I tried this (numfiles was part of some old code, but I can use it here to start a different process on the second circuit... guess what, the first time the thisapplication.actiview.fit line is triggered, same issue.  Interesting.



          Dim oView As View
            Set oView = ThisApplication.ActiveView
   If numFiles = 1 Then
                Dim oCamera As Camera
                 Set oCamera = oView.Camera
                  oCamera.Fit
                  oCamera.Apply
  
  Else
  
            
            ThisApplication.ActiveView.Fit
  End If
  
0 Likes
Message 5 of 7

MechMachineMan
Advisor
Advisor

Do you have your application as visible/ with screen updating while you are running it? I would assume disabling both of these would cause your program to run much faster seeing as it is dealing with graphics. Can be cumbersome to get them reset to enabled if errors are encounter, however.


--------------------------------------
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
Message 6 of 7

Anonymous
Not applicable

Yes, for now I'm running everything as visible as I debug my way into full automation, the dream is all of my work order creation runs silently on its own instance of inventor and I just kick back and have some Mai Tai's.

I fixed it completely, was obvious, from your link:

oCamera.ApplyWithoutTransition 

Thanks for the tips!


0 Likes
Message 7 of 7

Anonymous
Not applicable

Inventor VBA doesn't seem to sense acktime = 2, box remains open forever... Anyone program a self closing message box?  There are many ways but this one seemed elegant:


 Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
'Set the message box to close after 10 seconds
AckTime = 2
Select Case InfoBox.Popup("Drawings Exported", AckTime, "Closing Time", 0)
Case 1, -1
Exit Sub
End Select
0 Likes