Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 516

Power(Shell)ing the Inventor API!

$
0
0

By Sajith Subramanian


If you are more comfortable using PowerShell and have an Inventor API workflow that you want to quickly test, you can just combine both!

To help demonstrate this, below is a Inventor API sample in PowerShell that exports a drawing file as PDF.

Once you have your Windows PowerShell Integrated Scripting Environment (ISE) up, the first line of code would be to add the Inventor reference DLL as below:

Add-Type -Path ("C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Autodesk.Inventor.Interop\v4.0_25.0.0.0__d84147f8b4276564\Autodesk.Inventor.Interop.dll")


The next step would be to create an instance of Inventor (or get an existing instance if it is already open). To do so, you can add the below lines:

try { $m_inventorApp = [Runtime.Interopservices.Marshal]::GetActiveObject('Inventor.Application') $m_quitInventor = $false echo 'Getting existing instance...' } catch {
     echo 'Creating new instance...'
     $inventorAppType = [System.Type]::GetTypeFromProgID("Inventor.Application");
     $m_inventorApp = [System.Activator]::CreateInstance($inventorAppType)
     $m_quitInventor = $true

}


Now, we open our input drawing document with the “Open as Visible” flag set to False, and use this document object as input to the PublishPdf function.

$document = $m_inventorApp.Documents.Open("C:\TEMP\Hairdryer.idw", $false) PublishPdf($document)


Now, in our PublishPdf function we add the below lines:

function PublishPdf {

   param ($doc)

   $PDFAddIn = $m_inventorApp.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
   $oContext = $m_inventorApp.TransientObjects.CreateTranslationContext()
   $oContext.Type = [Inventor.IOMechanismEnum]::kFileBrowseIOMechanism

   $oOptions = $m_inventorApp.TransientObjects.CreateNameValueMap()

   $oDataMedium = $m_inventorApp.TransientObjects.CreateDataMedium()

   If ($PDFAddIn.HasSaveCopyAsOptions($doc, $oContext, $oOptions))
    {
    
       $oOptions.Value("All_Color_AS_Black") = 0
    }
   
   $oDataMedium.FileName = "c:\temp\test.pdf"
      
   $PDFAddIn.SaveCopyAs($doc, $oContext, $oOptions, $oDataMedium) }


And finally, close the created Inventor instance:

if ($m_inventorApp -ne $null -and $m_quitInventor -eq $true)
{
 $m_inventorApp.Quit()
 echo 'Quitting Inventor...'
} 


In the above example, while I have hard-coded the input file path, you can also choose to pass this as an input in a command line argument.


Viewing all articles
Browse latest Browse all 516

Trending Articles