Thursday, October 12, 2017

Saving a PDF of a Drawing Using iLogic

As you probably have noticed, Inventor iLogic rules are a frequent topic in this blog.  Sharing some ways I have used iLogic in the past is a subject I like sharing with others.  One of my first rules, that I was pretty proud of, was the ability to have iLogic save a PDF of Inventor drawings.  I tackled this because it is a task a large number of Inventor users need to perform.  I was able to figure out the code and I shared it in a YouTube video and in an Autodesk University class in 2012.  With the updated Event Triggers in Inventor 2018.1, I figured it might be time to updated the rule and share it with all of you.

The most popular question I got about the previous version was, "How do it get it to print all the sheets?"  By default, Inventor uses the last settings for one sheet or all sheets.  I had not figured out how to do that programmatically.  So I wanted to figure that out for this go round.

Like any other coder, I went to the web for help.  I pretty quickly found Curtis Waguespack's post.  So I used this code and my code to create what I call Franken-code (i.e. Frankenstein Code).

If you are interested in the code, you can download it from here.

The code is documented, but here is an explanation and demonstration of the code.


Happy Coding!!!

17 comments:

  1. Hello !
    I would like to know how to change the iLogic code to create PDF files in a project-specific folder. whenever I create a project, I create subfolders within the workspace.
    The folders are:
    Parts (ipt)
    Drawings (idw)
    Standards (ipt purchased)
    PDF (pdf)

    I would like the code to create the PDF folder inside the workspace.
    Could you please suggest how to proceed?

    Thanks a lot

    Paulo Melo - paulosbm52@gmail.com

    ReplyDelete
  2. Paulo,

    Part of the code is to set the save location. You would have to change the code that it automatically saves to your PDF folder. You would probably some the existing code to determine the Project's Workspace. Then append to that "\PDF\" to the workspace path and use that as your path for saving the file.

    Hope this helps,
    Steve

    ReplyDelete
  3. Hello
    On the PDF file name I want a prefix whit our company name and a suffix whit plate thickness, is that possible and can you help me whit doing that?

    Thanks
    Hans Kyrre
    And is there a ilogic rule that will also make a copy of the ipt file?

    ReplyDelete
    Replies
    1. The answer to each of your questions is, "yes."

      For the prefix and suffix, there is a lines that states:

      PDFPath = "C:\PDFs\" & DirectoryPath
      PDFName = PDFPath & "\" & ThisDoc.FileName(False) & ".pdf"

      These lines are what is generating your file name and saving directory. So if you wanted to you could hard code in your company name by stating:

      PDFName = PDFPath & "\MESA_" & ThisDoc.FileName(False) & ".pdf"

      In this case, MESA is the company name and I used an underscore just to separate it from the file name.

      For the Thickness, you would need to retrieve the plate thickness into an iProperty that you insert. This can be as simple as exporting the parameter that is the thickness of the plate. That is done by checking the Export box on the parameters. You would then have to append that into the name of the file. Assuming you export your thickness to a custom iProperty named THK, the new line would be:

      PDFName = PDFPath & "\MESA_" & ThisDoc.FileName(False) & "_" & iProperties.Value("Custom", "THK") "_.pdf"

      Again, I added underscores to the name to add some separation to make the name easier to read.

      As for your question about the saving a copy of an ipt, the FileSaveAs snippet, found under the Document branch of the tree. In this case, you will need to come up with a way of defining what the new name is.

      Hope this helps.

      Delete
  4. Hey, im trying to use your code, but i get a error:

    >>>Error in rule: Create+PDF, in document: 613.idw
    Argumentet 'Length' skal være større end eller lig med nul. (have to be bigger or
    equal to zero)<<<

    at Microsoft.VisualBasic.Strings.Right(String str, Int32 Length)
    at LmiRuleScript.Save_As_PDF()
    at LmiRuleScript.Main()
    at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
    at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

    thanks in advance

    ReplyDelete
    Replies
    1. Did you customize the code at all? I just downloaded the code from the link in the post and it ran fine for me. Also, the error says there is an issue with the value stored in "Length" and I looked through the code and their isn't a variable named length. If you did customize it, I would be happy to look at it. Just send it to steve.olson@mesa-cad.com and I will take a look.

      Thanks,
      Steve

      Delete
  5. Do You have a Solutions to flatten the drawing when is generating from Inventor?
    After open in Adobe Reader is open the panel which layers always.
    I don't want him. Can you have solution for this?.

    ReplyDelete
    Replies
    1. Andrew,

      I did a little research. As of right now, Inventor's Save As PDF function will save the layer information in the PDF. The suggestion has been logged as a wish list item for Inventor. You can see the idea and vote it up at this link.

      https://forums.autodesk.com/t5/inventor-customization/export-pdf-without-layers/m-p/5719762

      Other users have stated that if you print the file to a PDF printer, then it will not include the layers. I don't have a rule like this, however I noticed that there is Custom Snippet named Print Document that could be a good start to building your code. You also might find someone that has posted working code that does this.

      Thanks,
      Steve

      Delete
  6. Hello
    I read this post.
    I even voted for this idea to improve inventor.
    I was wondering if it is possible to save the Ilogic code so that it can communicate with an external PDF printer, e.g. PDF Creator for flattem drawing?

    ReplyDelete
  7. Hi!

    This is great, the snippets have helped a lot with other projects of mine.

    The Autodesk Viewer doesn't seem work. Can you re-upload or post it elsewhere?

    ReplyDelete
    Replies
    1. You can try this link which is just at TXT file.

      https://a360.co/36otGk0

      Delete
  8. Hello Steve,

    I want to firstly say thank you for your guidance on creating a seamless procedure to create a PDF from a drawing. However, I'm also encountering a similar issue to the one above (posted on 04/01/2020).
    When trying to run the iLogic code on a new drawing, the error code of "Argument 'Length' must be greater or equal to zero." appears. The length it's referring to is the "WorkspacePathLength" I believe. This issue is resolve once the drawing is manually saved first.

    Also I was wondering if it was possible to have the "PDFPath" unique to multiple systems. ie have the directory written in a way where the pdf will save to different user C:\Users\x

    Thanks in advance!

    ReplyDelete
    Replies
    1. Justin,

      Thank you for your comment. You are right, if the file's path is null because it hasn't been saved yet, you will get that error.

      Your second question provided an opportunity for me to learn. I haven't tried that before, so I did a little research. I found if you change path line to:

      PDFPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile)

      The iLogic rule will retrieve the users profile folder. C:\Users\. You can then append \Documents after that to get to their documents folder.

      One trick I use while I am debugging the path and name portion of the rule is that I add this line to the rule after I compile the name.

      MessageBox.Show(PDFPath, "Title")

      This will cause a message box to pop-up with the name you have built. Once you are confident you are getting the right name each time, you can either comment out that line or delete it. That line has saved me multiple times because I didn't realize the name was not what I was expecting it to be.

      Hope this helps,
      Steve

      Delete
  9. Hi,

    The link to your code doesn't work anymore I think.
    But I tried to just type it, but I am gettign errors.

    Any other place where I can find your code to copy?

    Kind regards

    ReplyDelete