5 Replies Latest reply on Sep 8, 2017 1:33 AM by Silly-V

    Cannot place 'PlacedItems()' a .dxf file

    clutch-cargo Level 1

      I would like to "place" a .dxf file into my document via a script but no luck so far .  With the script below, as a test, I can place a .tiff file with no issues but not a dxf.  I can Open the .dxf file but I need to Place due to a custom template.  I also can manually place the .dxf via the AI menus but again need to automate.  The script blow gives an AI Unknown Error when I try to run with a .dxf. (FYI - using  For/Next as I plan to Loop through a folder but now just testing with one file).

       

      Set App = CreateObject("Illustrator.Application")

      Set FSO = CreateObject("Scripting.FileSystemObject")

       

      Dim CustomTemplate : CustomTemplate = "S:\SOCAL\Section_32\_Control Data\SWS_template.ait"

      Set SourceFolder = FSO.GetFolder("S:\SOCAL\Section_32\Road DXFs")

       

      For Each FileRef In SourceFolder.Files

       

              Dim SourceFile, MyDoc

              SourceFile = "S:\SOCAL\Section_32\Road DXFs\SOCAL_CN68_resx.dxf"

              App.Open CustomTemplate

             

              Set MyDoc = App.ActiveDocument

              MyDoc.PlacedItems.Add().File = SourceFile

      Next

       

      I suspect it has to do with some custom AutoCad formatting/code needed but what I found in the AI samples did not make a difference.  I also thought of using the statement:

       

      App.ExecuteMenuCommand("AI Place")

       

      Which does work but I still have to select the .dxf file manually.  Maybe there is a way to have my code select the file???

       

      If someone would care to try to place one I would greatly appreciate it.  Though I code mostly in VBscript I would be more than happy to take a JavaScript.

        • 1. Re: Cannot place 'PlacedItems()' a .dxf file
          Silly-V Adobe Community Professional

          2017-08-30 13_21_46-Adobe Illustrator.png

          I get this error when trying with javascript. Therefore I infer that this is a similar situation to the SVG file type. What appears to be happening is, the UI dialog command differs from its scripting counterpart in that Illustrator will employ extra processes to 'place' these kids of file types. What I mean by 'extra processes' is it somewhere along the flow has to open the files up and get their contents in order to paste those contents into the file.

           

           

          As you can see in the UI dialog itself, the "Link" checkbox is disabled.

           

           

          And when such an item is 'placed' manually, you can see Illustrator used its invisible extra processes to structure the art and you get a resulting group with art in it instead of a placed item.

           

          So what can one do in this situation? I think the only way is to open the file, copy all the contents and move them over to the document you are placing into.

          • 2. Re: Cannot place 'PlacedItems()' a .dxf file
            clutch-cargo Level 1

            Well, glad to see I was not really doing it incorrectly as I was getting that error as well.  So you suggest:

             

            "I think the only way is to open the file, copy all the contents and move them over to the document you are placing into."

             

            Could you be a little more explicit?

             

            When you say copy I take it meaning READ the file in my script?

            When you say move them to you mean like Write?  Lost here... ??   If so, would it not still be in .dxf format?

             

            Any thoughts on the 'App.ExecuteMenuCommand("AI Place")' as far as selecting the file via the script rather than have to select manually?

            • 3. Re: Cannot place 'PlacedItems()' a .dxf file
              Silly-V Adobe Community Professional

              What I mean is, you simulate what the manual dialog does, with script. You do app.open(file) and it opens the file up, then you have your script copy over the art, then you have your script close the file.

              By 'move' the art, I mean using pageItem.move() command, or just using a select-all + copy + paste with scripting commands that do this.

              There's also the duplicate command, but if you're going to close the opened file without saving, it really makes no difference. Here's a JS to show an example of duplicate:

               

              #target illustrator

              function test(){

                var doc = app.activeDocument;

                var targetDoc = app.documents[1];

                alert("Active Document: " + doc.name + "\tTarget Document: " + targetDoc.name);

                var myArt = doc.pageItems;

                for(var i=0; i<myArt.length; i++){

                  myArt[i].duplicate(targetDoc.layers[0], ElementPlacement.PLACEATEND);

                };

              };

              test();

               

              It's fine to use the menu command to produce the manual dialog, if manually choosing the file or pressing "Cancel" is within your allowed project requirements, too.

              • 4. Re: Cannot place 'PlacedItems()' a .dxf file
                clutch-cargo Level 1

                Hey Silly-V

                 

                I spent a few days over the Labor Day weekend testing your script and I could not get it to run?  I tried it as a file with:

                 

                App.doJavaScriptFile = ("your script in a separate file")

                 

                and tried

                 

                App.doJavaScript = ("your entire script as a string")

                 

                I did not alter the scripts, both result in an "unknown error" but it did say source = null? I will say your idea above got my "thinking juices" flowing so with a combination of Copy/Paste with some AI Actions and some vbscript I was basically able to perform the same objective, granted not as elegant as your code.

                 

                Speaking of your code, I do not understand where the small "t" comes from with "\tTarget Document: "?  Also, I see the two plus signs"i++" like you have in 'for(var i=0; i<myArt.length; i++){'.  Is that a JavaScript thing?  Someplace I can read up on that?

                 

                Thx.

                • 5. Re: Cannot place 'PlacedItems()' a .dxf file
                  Silly-V Adobe Community Professional

                  the '\t' is a tab character, and ++ is a javascript operator shorthand for incrementing by 1. I suggest the W3C schools site for a quick javascript reference. Be aware that ExtendScript is ECMAscript version 3 while all browsers are now 5 and above, meaning that in Illustrator javascript, we do not have some of the features which come automatically in the later versions, such as Array.indexOf().