cp2641 - pdf, email notification, print, and more with...

15
PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs Marco Mirandola – coolOrange Wolfgang Hirner - coolOrange CP2641 The Autodesk Vault Jobprocessor comes with great jobs for updating the document and creating a DWF. Further more jobs can be created using .Net languages like C#. Such jobs might create PDFs, or sending emails, printing, exchanging data, and more. Most technicians have at least some experience with writing code or at least adapting existing code, even if it’s just with simple scripting. powerJobs, an extension for the Vault Jobprocessor, brings the Windows PowerShell scripting language into the game. This makes it easy for amateur and professional developers to extend existing and create new jobs. This tutorial will guide you through the basics of the Valult Jobserver and show you how you can achieve the jobs described above via powerJobs. Learning Objectives At the end of this class, you will be able to: Learn all tools around Jobprocessor, and the powerJobs extension Learn how to create PDFs from AutoCAD and Inventor files, with many variations Learn how to create automatic email notification to improve your communication Learn how to create jobs for printing, conversion, exchanging About the Speaker Marco is CEO and founder of coolOrange, an European based company focused on creating software for Autodesk products and provide services to Autodesk resellers. He is now for more then 15 years in the Autodesk eco-system and worked for 4 years as Product Manager for Data Management at Autodesk. His and his team experience ranges from implementation, on configuration to customization, in many small and big projects around the globe. [email protected] www.coolorange.com

Upload: dangdung

Post on 20-Aug-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs Marco Mirandola – coolOrange Wolfgang Hirner - coolOrange

CP2641

The Autodesk Vault Jobprocessor comes with great jobs for updating the document and creating a DWF. Further more jobs can be created using .Net languages like C#. Such jobs might create PDFs, or sending emails, printing, exchanging data, and more. Most technicians have at least some experience with writing code or at least adapting existing code, even if it’s just with simple scripting. powerJobs, an extension for the Vault Jobprocessor, brings the Windows PowerShell scripting language into the game. This makes it easy for amateur and professional developers to extend existing and create new jobs. This tutorial will guide you through the basics of the Valult Jobserver and show you how you can achieve the jobs described above via powerJobs.

Learning  Objectives  At the end of this class, you will be able to:

• Learn all tools around Jobprocessor, and the powerJobs extension

• Learn how to create PDFs from AutoCAD and Inventor files, with many variations

• Learn how to create automatic email notification to improve your communication

• Learn how to create jobs for printing, conversion, exchanging

About  the  Speaker Marco is CEO and founder of coolOrange, an European based company focused on creating software for Autodesk products and provide services to Autodesk resellers. He is now for more then 15 years in the Autodesk eco-system and worked for 4 years as Product Manager for Data Management at Autodesk. His and his team experience ranges from implementation, on configuration to customization, in many small and big projects around the globe.

[email protected] www.coolorange.com

Page 2: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

2

Tools  for  Jobprocessor  and  powerJobs  In this section we will have a quick look to several tools around Jobprocessor and powerJobs. For the Jobprocessor, we will quickly see the settings within Vault, have a look to the Jobprocessor configuration file and see the LifecycleEventEditor. For powerJobs we will quickly have an insight into the Windows PowerShell scripting language, as it is the foundation of powerJobs, and discover where the relevant files and information are located.

The  Vault  Jobprocessor  settings  In Vault you have the ability to turn on and off the Jobprocessor queue. In order to queue jobs, it has to be turned on. You can find this setting under [Tools]>[Administrator]>[Global Settings]. On the [Integrations] Tab you will find the “Enable Job Server” checkbox. Make sure this is checked.

Via the menu [Tools]>[Job Queue…] you can have a look into the queue and see which jobs are pending, processing, or in error.

The  Jobprocessor  configuration  The configuration file Jobprocessor.exe.config, which is located in the Explorer folder nearby the Jobprocessor.exe, contains the definition for which jobs should be executed by the Jobprocessor, and more. It is an XML file. To open that file, we can use a simple notepad, or better we download the free XML Notepad from the Microsoft website (http://www.microsoft.com/en-us/download/details.aspx?id=7973).

Page 3: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

3

The section JobHandlers describes which jobs the Jobprocessor knows.

The list of jobs described here is the list of jobs you will see in the [Administration]>[Job Types…] dialog of the Jobprocessor.

By changing a jobHanlder component in the config file to a comment, the job type will disappear from the Job Types list in the Jobprocessor, and that job will not be processed on that Jobprocessor.

We will edit this file later, as we will add custom jobs.

The  LifecycleEventEditor  The Lifefycle Event Editor is a useful application that comes with the Vault SDK. It allows us to add custom jobs to the lifecycle transition. So, for instance if we like to send an email on a specific lifecycle transition via the jobserver, we can use the LifecycleEventEditor to configure this.

Page 4: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

4

In order to use this tool, the SDK must be installed. You can find the setup under C:\Program Files\Autodesk\ADMS Professional 2013\SDK, or in the folder where you have installed the AMDS Server. After running the Setup you will find under C:\Program Files (x86)\Autodesk\Autodesk Vault 2013 SDK the content of the SDK. The LifecycleEventEditor is located in the Util folder. Just start the tool, login with your Vault credentials, select the lifecycle and the transition you look for, and add the job of interest.  

PowerShell  and  PowerGUI    As Windows PowerShell is the foundation of powerJobs, lets have a look what this is and which tools are available.

On your Windows 7 machine, you will find under [Start]>[All Programms]>[Accessories]>[Windows PowerShell] the PowerShell command line and the PowerShell ISE (Integrates Scripting Engine).

Via the command line, it looks like the old DOS days, you can run PowerShell commands interactively. The PowerShell ISE, allows you to create scripts, which will be then executed via the Windows Explorer, or other Windows applications. The scripts can be save with the extension .ps1.

Page 5: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

5

The PowerShell is also available for earlier version of Windows as download from the Microsoft website. On the other hand, it’s a central component for the current and upcoming Windows operating systems, like Windows Server 2008 R2, Windows 8 and more.

As an alternative to the tools coming with the operating system, there are other PowerShell editors that bring more capabilities. We will use the PowerGUI (http://www.powergui.org). This editor comes with the so-called “intellisense”, which means it suggests me how to complete the commands I’m writing. This simplifies coding and reduces errors.

First  steps  with  PowerShell  As the rest of this workshop will be with PowerShell, let’s spend a few minutes to get familiar with this language.

By running the PowerShell command line, you will notice that it really looks like a DOS command prompt. You actually can use commands like cd for changing directory or dir, copy, etc. However, a new naming has been introduced which should make the PowerShell commands more intuitive. A verb and an object compose the PowerShell commands. Like set-location is the new name for cd. The Get-Command will give you the whole list of available so-called commandlets or cmdLets. Via Get-Help you can get the help and by adding the commandlet you are interested to, you get the help to that one, including samples.

Here is a 2 page overview about the most common PowerShell commands: http://www.microsoft.com/en-us/download/details.aspx?id=7097

An here a quick and nice getting started site: http://technet.microsoft.com/en-us/library/ee177003.aspx

The  pipeline  PowerShell offer great ways for computing objects and lists (arrays) of elements, filter them, sort them, and transform them to different formats. For instance we can get a list of files from a specific folder including subfolder, filter the files newer then a specific date, sort them by name and write them out to a CSV file, in order to open the output in excel.

Page 6: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

6

The Get-ChildItem commandlet will collect all the files from a specific folder. With the -recurse argument, all files will be captured. The result will be stored in a variable, which starts with a $ sign in PowerShell. The content of the variable will then be filtered and sorted by using the pipeline ( | ) and some additional commands.

Let’s start by collecting all files from the Program Files folder:

$files = Get-ChildItem 'c:\Program Files' –Recurse

Now let’s filter the collected files for those that has been modified in the last 30 days

$today = Get-Date $last30days = $files | Where-Object { $_.LastWriteTime -gt $today.AddDays(-30) }

Now let’s sort them by name

$last30days = $last30days | Sort-Object Name

Page 7: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

7

Now let’s export them to a CSV file in order to open the result with Excel

$last30days | Export-Csv c:\Temp\last30days.csv -Delimiter ";"

Page 8: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

8

We can save these commands as a ps1 file and execute the script whenever we want. This could be a simple sample on how to find changed files on the local system in an automated way.

But, why should we care? What does have this something to do with Vault and Jobserver?

PowerShell  and  Vault  In a similar way as we used PowerShell to interact with the operating system, we could also interact with Vault. We just have to load the Vault API and login into the Vault server. The needed file is part of the Vault SDK we just installed before. And here is how it works:

[System.Reflection.Assembly]::LoadFrom("c:\Program Files (x86)\Autodesk\Autodesk Vault 2013 SDK\bin\Autodesk.Connectivity.WebServices.dll") $cred = New-Object Autodesk.Connectivity.WebServicesTools.UserPasswordCredentials("localhost","Vault","Administrator","") $vault = New-Object Autodesk.Connectivity.WebServicesTools.WebServiceManager($cred)

These 3 lines, are loading the Vault API and login into Vault, with the credentials you will give. The result is a $vault object, which allow us to interact with Vault. A simple example could be to create a folder in Vault via PowerShell.

$rootFolder = $vault.DocumentService.GetFolderRoot() $newFolder = $vault.DocumentService.AddFolder("coolOrange",$rootFolder.Id, $false)

The first line is getting the root folder from Vault, which is the folder called $. The second line does add a folder to Vault. The requested arguments are the folder name, which we will call coolOrange, the parent folder and whether the folder should be a library folder or not. If you run these commands and then check your Vault, a new folder called coolOrange should be under $.

In similar way as we did for the files on the files system, we can query the Vault for files, filter and sort the result and transform it to CSV or similar. Here the code:

$folder = $vault.DocumentService.GetFolderByPath("$/Designs/Padlock/Assemblies") $files = $vault.DocumentService.GetLatestFilesByFolderId($folder.Id, $false) $files = $files | Sort-Object Name $files | Export-Csv c:\temp\last30days.csv -Delimiter ";"

Page 9: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

9

Here we ask Vault for the files from the folder $/Designs/Padlock/Assemblies. Of course this path can be adapted and other functions like the search can be used. The result is then sorted by name and exported again as CSV.

So, now you see how easily the Vault data can be queried and also modified via PowerShell by using the Vault API. As the powerJobs jobs are written with PowerShell language, you may start imagine the things that can be done.

PDF  with  powerJobs  Once powerJobs has been installed, standard jobs have been installed too. You will notice in the jobprocessor under [Administration]>[Job Types…] that at the end, the coolOrange powerJobs jobs have been added.

One of these jobs creates PDF of your Inventor and AutoCAD files. The according script is called coolOrange.powerJobs.CreatePDFAsAttachment.ps1 and is located under c:\ProgramData\coolOrange\powerJobs\Jobs. We will use the LifecycleEventEditor for configuring our PDF job to be triggered once the file gets released. The name of the job that we will add to the given lifecycle state, is the one you will find in the jobprocessor.exe.onfig under JobHandlers. The attribute Name contains the text we look for and we will add via the LifecycleEventEditor to the release state. Once this is completed, we only have to release an

Page 10: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

10

Inventor or AutoCAD document in Vault, and let the Vault Jobserver run. After completion, a PDF of your document will be in Vault nearby the original document.

Now, in different projects, at different customer, different requirements will arise. So, the next steps will show variations of our PDF, in order to get a feeling of the bandwidth.

PDF  with  same  category,  revision  and  lifecycle  The standard PDF does not have any particular characteristics. Via the Vault Category Rules, you may apply first basic characteristics. In order to have the PDF in the same category, revision and lifecycle, a few changes will have to be applied to the standard job. Here are the 3 relevant lines:

$newFile = $vault.DocExtService.UpdateFileCategories(@($newFile.MasterId),@($file.Cat.CatId),"updated category") $newFile = $vault.DocExtService.UpdateFileRevisionNumbers(@($newFile.Id),@($file.FileRev),"Updated Revision") $newFile = $vault.DocExtService.UpdateFileLifeCycleDefinitions(@($newFile.MasterId),@($file.FileLfCyc.LfCycDefId),@($file.FileLfCyc.LfCycStateId),"updated lifecycle")

These 3 lines basically update the category, revision, lifecycle and state of the PDF to the one of the original file.

 

PDF  with  the  revision  in  the  name  In other situations, the requirement might be of having the PDF with a different file name, for instance without the extension of the original file and maybe with the revision in the name. This way, the PDFs of different revisions, will be all immediately visible in Vault.

Page 11: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

11

At the very beginning of the PDF script, there is a section called settings. There is a variable called PDFfileName. That variable drives the file name of the PDF. By default the file name is the name of the original file with additional the extension .pdf. The next line takes just the name of the files without the extension and ads an underscore, the revision of the original file and finally the extension.

$PDFfileName = [System.io.path]::GetFileNameWithoutExtension($file.Name) + "_" + $file.FileRev + ".pdf"

PDF  in  an  external  folder  A frequent request is to create a PDF and make this accessible to other systems like an ERP. Now, with what we have seen before with PowerShell and Vault, you may know that a small script could grab the PDF from Vault and save it to the specified folder. However, as the PDF gets created via a job, why not having the job save the PDF to that location, right during the creation process.

As by default the PDF must be created locally anyway, and just uploaded to Vault later, we could just grab the local file and copy it to your location. The variable $localDestFile contains the location of there the PDF will be created. After the successful creation, let’s copy the file:

Copy-Item -LiteralPath $localDestFile -Destination "c:\myFolder\"+$PDFfileName

Page 12: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

12

PDF  with  Watermark  and  PDF-­‐A  Beyond the basic naming or managing of the PDF, there might be a whish to create PDF with watermark, for instance reflecting the lifecycle state on the PDF. Or you may want to create a PDF-A format of the PDF for long term archiving.

So far the creation of PDF has been made via Inventor or AutoCAD, which can create regular PDFs. In order to add watermark to the PDF or create a specific format, additional tools are needed. There are different on the market. We choose iText (http://itextpdf.com) for manipulating PDF and so add Watermark, or even other manipulations, and Ghostscript (http://sourceforge.net/projects/ghostscript/) for creating PDF-A. In both cases the PDF generated by the Autodesk application will be post-processed with the tools of above.

As we are constantly extending the reach of samples, for these 2 situations we like to point you to the tutorials we wrote on our wiki site:

- PDF-A: http://wiki.coolorange.com/index.php?title=powerJobs/5._Questions_%26_Answers/pdf_to_pdf//a - Watermark:

http://wiki.coolorange.com/index.php?title=powerJobs/5._Questions_%26_Answers/Add_a_watermark//stamp_to_pdf

Email  notification  Another great way to use the Vault Jobserver is to improve the automatic notification of Vault and Non-Vault users. Especially when documents gets or leave a specific lifecycle stage, people may want to be informed. The Vault Jobserver can take over this task, and with powerJobs, it’s just a simple task to create a little PowerShell script that will send emails or other types of notification like twitter or yammer tweets.

Notification  via  Email  In order to send emails, you need a SMTP server. So, we assume this is given and you have the credentials. Then, it’s an easy game. PowerShell comes with a commandlet called Send-MailMessage. Jus add the given arguments like from, to, subject, body and a valid SMTP server, and you have emails sent by the Jobserver at lifecycle transitions. The content of the

Page 13: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

13

email can be configured as needed. Here a sample that sends an email with the file name with from and to state in the subject, and a Vault link in the body. If the user clicks on the link, Vault will start and point to the according file.

$lfcTransId = $vault.Job.Params["LifeCycleTransitionId"] $fileID = $vault.Job.Params["FileId"] $file = $vault.DocService.GetFileById($fileID) $folders = $vault.DocService.GetFoldersByFileMasterId($file.MasterId) $folder = $folders[0] $folderPath = $folder.FullName $folderPath = $folderPath.Replace("$","%24").Replace("/","%2f") $fullPath = $folderPath + "%2f" + $file.Name.Replace(" ","+") $link = "http://localhost/AutodeskDM/Services/EntityDataCommandRequest.aspx?Vault=Vault&ObjectId="+$fullPath+"&ObjectType=File&Command=Select" $lfcTrans =$vault.DocExtService.GetLifeCycleStateTransitionsByIds(@($lfcTransId)) $lfcs = $vault.DocExtService.GetLifeCycleStatesByIds(@($lfcTrans[0].FromId,$lfcTrans[0].ToId)) $oldState = $lfcs[0].DispName $newState = $lfcs[1].DispName Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject "The file $($file.Name) has changed from $oldState to $newState" -Body "Dear xxx, if you like to view the related document, just follow this link: $link" -SmtpServer 10.0.0.18

Notification  via  Yammer  If Yammer is used in your company, then you might be interested to connect Vault to this business social network. Yammer offers an API in order to allow developers to connect to this network. We run into this article http://blogs.msdn.com/b/johnmill/archive/2012/09/10/yammer_2d00_from_2d00_the_2d00_command_2d00_line.aspx which shows how PowerShell can take advantage of Yammer. The Set-Yammer-Message commandlet sends a message to your Yammer network. So, for instance at given lifecycle changes in Vault, you may want to notify people via Yammer.

In order to do this, follow the tutorial mentioned above, create a new PowerShell file and place it in the powerJobs/Jobs folder, add a jobHandler in the Jobprocessor.exe.config and edit the PowerShell script with the following code:

Set-Yammer-Message –Message “here comes your message”

Of course you can use all the Vault information to format your message the way you want, similar to how we did it for the email notification.

Printing  and  Converting  Another excellent topic for the Vault Jobserver is to centralize printing and converting.

Create  DXF  on  validation  Supposing you like to get your flat pattern DXF create automatically when releasing your drawing. One of the default jobs that comes with powerJobs, does exactly this for Inventor files. The script responsible for this is coolOrange.powerJobs.SaveLocalAsSheetMetalDxf.ps1.

This script is set to IPT only. It checks whether the Inventor component contains a flat pattern. If not, then the model will be unfolded and saved. Then a DXF file will be generated with the given parameters.

Page 14: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

14

The file will be just stored in a local Windows folder. From here on further actions can be taken.

Create  BMP,  STEP,  IGS,  …  The script for generating a DXF shows a technique that allows intervening once Inventor has opened the file. At that time it’s possible to interact with Inventor and the opened file. One simple thing that can be done is to save the file in a different format. We basically make use of the SaveCopyAs function from within Inventor. The format supported by that function are available also from the API.

The add_OnBeginPublish function catches the opening of the file and provides some useful variables, like the current opened document. Within this function we can save our document to the path and file we want. The extension will automatically tell Inventor which format to use. Here the command:

$document.Document.SaveAs("c:\temp\inventor.step",$true)

Unfortunately the settings cannot be passed to this function, so the export will happened exactly with the settings chosen the last time a document has been saved with this format.

Print  via  Inventor  In order to print a file via Inventor, a similar approach to the one for creating a DXF can be taken. In this case we just open the file, catch the open event and then trigger the print. For printing, the PrintManager from Inventor is used. This allows us to be quite flexible on the priting requirements. Here is the code:

# get the file $fileID = $vault.Job.Params["FileId"] if(!$fileID) { throw ("File ID not set") } $file = $vault.DocService.GetFileById($fileID) # get the latest version of the file in case a sync prop has been executed bevore the job $file = $vault.DocService.GetLatestFileByMasterId($file.MasterId) # limit publishing to 2d inventor files $ext = [System.IO.Path]::GetExtension($file.Name) if($ext.ToLower().Equals(".idw")) { $publisher=$vault.GetPublisher("PDF") $publisher.add_OnBeginPublish( { param($publisher, $document) $printManager = $document.PrintManager $printManager.GetType().InvokeMember("Printer",[Reflection.BindingFlags]::SetProperty, $null, $printManager, "Microsoft XPS Document Writer") $printManager.ScaleMode = [Inventor.PrintScaleModeEnum]::kPrintBestFitScale $printManager.PrintRange = [Inventor.PrintRangeEnum]::kPrintAllSheets $printManager.PaperSize = [Inventor.PaperSizeEnum]::kPaperSizeA0 $printManager.SubmitPrint() }) $publisher.OutputFile="c:\temp\dummy.pdf" if (!$publisher.Open($file.Id)) { throw ("Open failed") } }

Page 15: CP2641 - PDF, Email Notification, Print, and More with ...aucache.autodesk.com/au2012/sessionsFiles/2641/2521/handout_2641... · We just have to load the Vault API and login into

PDF, Email Notification, Print, and more with Autodesk® Vault Job Processor via powerJobs

15

Conclusion  This workshop had the intention to give you a feeling of the many things that are possible via the Vault Jobserver, how PowerShell can lower the threshold for writing jobs, and how powerJobs leverages this.

The web is full of PowerShell communities and information, so before you write, just search. We contribute via out blog (http://blog.coolorange.com) to topics relevant to Vault. So, if you are now interested, start reading and testing your self, and feel free to send us suggestions on further topics we should work out. Leave a message on our blog, facebook site or support.