Issue

Per default powerJobs uses DWGTrueView to generate files out of AutoCAD drawings. DWGTrueView is able to export only PDF and DWF files. When other file formats are required it is necessary to use AutoCAD. The AutoCAD API runs in a single threaded apartment(STA). In order for a thread, this means a job, to be able to communicate with it it must run in a STA as well. However powerJobs starts the PowerShell runspace in a multi threaded apartment(MTA).


Solution

To be able to have a stable communication with the AutoCAD API some messagefilters need to be used. Without these you will get random COM Exceptions.


MessageFilters

  • Download MessageFilters.zip from the attachments
  • Make sure the files are Unblocked
  • Copy the files in your powerJobs Modules folder at "C:\ProgramData\coolOrange\powerJobs\Modules"
  • Make sure the Windows user that will run powerJobs has permissions to add, edit and delete files in the powerJobs modules folder


Using the Modules

The module AutoCAD.Helper.psm1 loads the latest AutoCAD assembly and does the communication with the AutoCAD API for you. The MessageFilter.psm1 and InvokeApartmentstate.psm1 are used for the communication with the AutoCAD API.

  • Before you can use the message filters you have to add them to the current runspace. Call the function Load-AutoCADMessageFilter to do that.
  • The fully qualified name of the class is cOCommunication.MessageFilter and it contains to static methods Register() and Revoke()
  • You always have to call the Register() method before you use any AutoCAD API call.
  • You always have to call the Revoke() method after you used your last AutoCAD API call.
  • You can open a new AutoCAD instance with the New-Object commandlet.


Example

This example shows how to export a DXF from an AutoCAD file using the provided modules. This can be added to your job to create a DXF file out of an AutoCAD drawing. The sample has to be inserted at least after the Save-VaultFile cmdlet if you are modifying a sample job delivered with powerJobs.

Invoke-Script -apartmentState STA -script {
    try {
        $Application = Start-AutoCAD
        $Application.Visible = 0

        $Document = $Application.Documents.Open($file.LocalPath)
        
        $Document.SaveAs($localDXFfileLocation, [Autodesk.AutoCAD.Interop.Common.AcSaveAsType]::ac2000_dxf, $null)

        if( -not ([System.IO.File]::Exists($localDXFfileLocation)) ) {
            throw $Error[0]
        }

        $DXFfile = Add-VaultFile -From $localDXFfileLocation -To $vaultDXFfileLocation -FileClassification None -Hidden $hideDXF
        $file = Update-VaultFile -File $file._FullPath -AddAttachments @($DXFfile._FullPath)
    }
    catch {
        throw "Error in Script! message: '$($Error[0])'"
    }
    finally {
        Close-AutoCAD
        
    }
}


Remarks

  • As the Invoke-Script commandlet creates a new thread within your current runspace you'll have access to all the objects and variables that were declared before calling it.
  • Please note that we cannot offer support for the AutoCAD API in the scope of product support, but you can post any questions in our forum if you want. If you need one of our supporters to work out a solution you will need a support card.
  • If you are using Powershell 2 you will have issues accessing the AutoCAD API without reflection. You should update the the latest version.