Book a Demo

Author Topic: Script sample: Create linked documents from Notes  (Read 6165 times)

fwoolz

  • EA User
  • **
  • Posts: 435
  • Karma: +0/-0
  • We have met the enemy, and he is us.<Pogo, 1970>
    • View Profile
Script sample: Create linked documents from Notes
« on: June 25, 2010, 12:06:45 pm »
I just had the need to do just what is described in the subject, so I cooked up a quick VBScript to do the job. I thought I'd post it for anyone who might be interested:

Code: [Select]
' AddDocs.vbs
' Script to create RTF documents for each element in a package from element Notes

!INC Local Scripts.EAConstants-VBScript

dim pkg as EA.Package
dim elem as EA.Element
dim fileSystemObject
dim inputFile
dim outputFile

sub main
      set fileSystemObject = CreateObject("Scripting.FileSystemObject")
      ' Contents of file RTFSampleStripped: "{\rtf1\ansi\deflang3081\ftnbj\uc1\deff0\plain @\par}"
      ' @ is a placeholder, to be replaced by Notes text
      set inputFile = fileSystemObject.OpenTextFile("C:\\Users\\fred\\Projects\\Specifyx\\RTFSampleStripped.rtf")
      rtf = inputFile.ReadAll()
      inputFile.Close
      set pkg = Repository.GetPackageByGuid("{C1444569-06CC-470d-8CD6-9FDB42D8A6A3}")
      for each elem in pkg.Elements
            notes = elem.Notes
            srtf = Replace(rtf,"@",notes)
            set outputFile = fileSystemObject.OpenTextFile("C:\\Users\\fred\\Projects\\Specifyx\\RTFSampleStuffed.rtf", 2, True)
            outputFile.WriteLine(srtf)
            outputFile.Close
            ok = elem.LoadLinkedDocument("C:\\Users\\fred\\Projects\\Specifyx\\RTFSampleStuffed.rtf")
            elem.Update
      next
end sub

main

Each Notes field is a single paragraph in this case; otherwise, paragraphs would need to be delimited by inserted RTF '\par' command words for paragraphs to break properly in the linked document.

Note that the variable rtf (contents read from the input file) could just as easily be stored in the code itself as a string (but don't forget to escape the backslashes!). Use of a file has the advantage that the rtf can be changed in the file without editing the script itself; also, you don't need to worry about escaping backslashes. Note also that only bare-bones (but correct!) RTF is required to successfully load RTF from a file into an EA element.

This would all be MUCH simpler if Sparx would make it possible to load a linked document directly from a string!
« Last Edit: June 25, 2010, 12:08:44 pm by fwoolz »
Fred Woolsey
Interfleet Technology Inc.

Always be ready to laugh at yourself; that way, you beat everyone else to the punch.


Centurio

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: Script sample: Create linked documents from No
« Reply #1 on: June 25, 2010, 07:08:13 pm »
Hi Fred,

thank you for that script.  8-)
Maybe a way to bypass the RTF-Report lack what I've described before within that forum.

Centurio

peuhhhh

  • EA User
  • **
  • Posts: 31
  • Karma: +0/-0
    • View Profile
Re: Script sample: Create linked documents from No
« Reply #2 on: July 09, 2010, 10:54:54 pm »
Hello fwoolz,

i'd like to use the same feature you are describing , but it seems you are importing another script which help you with the CreateObject method.
Code: [Select]
!INC Local Scripts.EAConstants-VBScript
maybe i'm wrong but in Jscript i dont have such a method.

if anyone else succeeded in Jscrip i'm strongly interested.

thanks in advance

peuhhh


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Script sample: Create linked documents from No
« Reply #3 on: July 09, 2010, 11:01:33 pm »
peuhhh,

One of the Sparxians posted a forum topic/reply a while ago which defines the EA constants.
Try looking for that.

Geert

peuhhhh

  • EA User
  • **
  • Posts: 31
  • Karma: +0/-0
    • View Profile
Re: Script sample: Create linked documents from No
« Reply #4 on: July 09, 2010, 11:14:47 pm »
thanks you Geert for you fast answer.
After searching the forum with keywords like : EA constants , EA constant or constant i do not found any relevant topics.

do you have any idea how i can find it?
thanks
peuhhh

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Script sample: Create linked documents from No
« Reply #5 on: July 09, 2010, 11:30:37 pm »
Look at "C:\Program Files\Sparx Systems\EA\Scripts\JScript"
or whatever your isntallation folder is.
I can't seem to recall where I read it, but constants have been recently added to the examples.

Geert
« Last Edit: July 09, 2010, 11:30:54 pm by Geert.Bellekens »

peuhhhh

  • EA User
  • **
  • Posts: 31
  • Karma: +0/-0
    • View Profile
Re: Script sample: Create linked documents from No
« Reply #6 on: July 09, 2010, 11:35:22 pm »
thanks Geert, looking at the examples, it seems I/O handling is only possible in VB.
i'll give it a try.

best regards,
peuhhh

peuhhhh

  • EA User
  • **
  • Posts: 31
  • Karma: +0/-0
    • View Profile
Re: Script sample: Create linked documents from No
« Reply #7 on: July 09, 2010, 11:53:22 pm »
in fact it is possible in Jscript.
here is a piece of code :

Code: [Select]
var ForReading = 1, ForWriting = 2, ForAppending = 8;

var fso = new ActiveXObject("Scripting.FileSystemObject");

// Open the file for output.
var filename = "c:\\testfile.txt";


// Open the file for input.
f = fso.OpenTextFile(filename, ForReading);

// Read from the file and display the results.
while (!f.AtEndOfStream)
    {
    var r = f.ReadLine();
    Session.Output(r + "<br />");
    }
f.Close();