cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Read a text file into a variable

JoseValdes
1-Newbie

Read a text file into a variable

From an ISM file, I want to get the content of a text file and assign it to a variable. The following code example is from a VBA script that does the same thing:
0658b030f7
Basically, I want to translate the previous VBA code to IsoDraw Macro (ISM) Language. Can anyone help? Thanks in advance, Jose Valdes PS - I'm sorry that I am posting such a newbie question, but I wasn't able to find this answer in the online help.
16 REPLIES 16

Side comment, but I'm not a fan of this new forum set up. As you can see, I just cleared most of the comments on this topic without much effort. Sorry.

Here's my recommendation (again).

Place the attached .bat file in C:\temp. Add the .ism as a macro. What this does is creates a temporary text element, makes a mock text excerpt to import in the .bat file that allows you to put your own value in. The file is opened as a text excerpt which updates the temporary text element. The value is then assigned to a variable, text element deleted, saves file with new name in C:\temp, and then displays message to user that file has been saved.

Sorry again for clearing the conversation. Meant to remove only my error from earlier in the thread and ended up clearing my, and everyones comments.

Well, maybe I'm doing something wrong but from your .bat & .ism I get back "TEMP" as the message instead of the string "ArtNumber_from_Perl" that's in the .dat file that gets created from your .bat file.

I'm assuming you just copied the files and ran the macro.

I tried it again on my machine and it worked, so let's troubleshoot some. Note that once you run the macro, you shouldn't click anywhere as this can cause problems (you can account for some of this, but this a rough draft macro).

1) Open the bnumout.dat file and see if the contents match below. If not, you might need to tweak the .bat file (I'm working on WindowsXP).

%IsoDraw Text
%Title: Temp.iso
0000033489 "ArtNumber_from_Perl"

2) Add the following message command at line 43 of the macro. The value should be a ten digit number.

Message Export_ID

3) Add the following message command at line 51 of the macro. The value should be "ArtNumber_from_Perl". At this point, also look in the lowerleft corner of the screen and make sure the text element matches. If they do not, let me know.

Message E1.String.Text

Thought about this issue more. Here are the likely issues you're running into.

1) The bat file is not in the correct folder (C:\temp) and thus is not being called out.

2) The call to cmd in the launch command is not correct for your machine. Check the path given to see if the file exists.

I check both of these suggestions. cmd.exe is in C:\WINDOWS\system32, the .bat file is in C:\Temp as well as the bnumout.dat file that gets created. However the contents of the bnumout.dat file are:

%IsoDraw Text

%Title: Untitled

- "ArtNumber_from_Perl"

It looks like the Export_ID is not being written to the bnumout.dat file. The Export ID value, just prior to the Launch command is "0000004434". The "Open"command displays the message "This text excerpt belongs to the illustration 'Untitled'. Doyou want to import it anyway", I click Yes. The value read from the input file is "TEMP" , Not the Export_ID

Grab the updated macro I posted (the original had an issue). The reason and fix is explained below.

The problem is that the file you are working on is new and not named (thus the "Untitled" in the export). I added a code to save the file as C:\temp\Temp.iso at the start of the macro. The text file should then contain "Temp" instead of "Untitled". If you still have a problem with the ID let me know. I'm guessing that issue will be resolved by default once the first issue is addressed.

Were you able to get it working?

Just got it working today...(been on vacation).

It's a bit quirky but I need to do more testing. The first time I run the macro in an IsoDraw session, it works fine. 'Sometimes' (haven't determined exactly the condition) if I run the macro more than once in a session, it seems to ignore the art number from the perl script and defaults to TEMP instead (even though the correct art number is written to the bnumout.dat file)

Just got IsoDraw 7.1 installed the other day and am working through basic testing. Found a great new feature that can do this even simpler. The text you want to import needs to be in a text file at the following path C:\temp\Temp.txt. At this point, if your source has blank lines they are dropped. Just need to check the string to see if it is blank, and if so, add two $NewLine commands, but I haven't gotten quite that far.

With the macro below, have a text element selected when you run the macro. This will then update the selected text element with the source text.

Macro Import_Text

E1 = ActiveDoc.FirstSelectedElement

E1.Text.String=" "

Batch "C:\temp\Temp.txt" "Get_Stuff"

End Macro


Submacro Get_Stuff (string Gotit)

If Gotit = " "
E1.Text.String = E1.Text.String + $NewLine + $NewLine

Else

E1.Text.String = E1.Text.String + $NewLine + Gotit

End If

End Submacro

Hello,

I am new to Macros, and I am required to generate a macro that is a little out of my depth at the moment and I would appreciate any assistance. First I tried the above macro and I get the following error:

Macro error: Missing brace.

"If Gotit = "''"

I need to accomplish many things with my macro, some are easy depending on the file structure. Hopefully i will know later today. I feel I will be able to achieve most task via Macro from what I have learnt from the reference. My main hurdle is accessing data from an excel sheet placing it into a already existing CGM, saving the file then moving onto the next information in the excel sheet and importing into another existing CGM and repeating this process.

I have seen Perl being used, I dont know Perl, but maybe Perl can be used to access the Excel doc where as IML alone cannot? Maybe I can export the Excel doc into a PDF or other format Isodraw can read?

Any advice assistance is greatly appreciated!

Thank you

Alan

My first thought on the error is that, in order for this macro to work, you must have a text file called "Temp.txt" in C:\temp\. You can, of course, modify this, but that would immediately give you an error.

In regards to your needs for this macro, I recommend starting a new post once you have a grasp of what you are trying to accomplish. If you have sample information that would be a good start as well as the major tasks you need to accomplish.

Thanks Trevor,

Below is the link to a new post:

http://communities.ptc.com/thread/33385?prevContainerType=14&prevContainerID=2038

Regarding the temp file, it is where you instrcuted to put it.

Thanks in advance.

Alan

Message was edited by: Alan Porter

I'll take a look at your post after a bit. In regards to the issue you had with this particular instance, I'd recommend taking a look at the value of GotIt prior to the if statement. Either add a message right before or use the debug to step through.

What were the contents of the txt file? There are restricted characters (can't remember off the top of my head) that might have returned errors. I believe two were single and double quotes. Preprocessing might be necessary.

Add a MESSAGE command, good one - why didnt I think of that! Ok this is the ammended ISM file:

Macro Import_Text

#added

DEFINE E1 as String

E1 = ActiveDoc.FirstSelectedElement

E1.Text.String=" "

Batch "C:\temp\Temp.txt" "Get_Stuff"

End Macro


Submacro Get_Stuff (string Gotit)

#added

DEFINE Gotit as string

MESSAGE Gotit

If Gotit = " "
E1.Text.String = E1.Text.String + $NewLine + $NewLine

Else

E1.Text.String = E1.Text.String + $NewLine + Gotit

End If

End Submacro

I orginally tried it without definining any and an error appeared in the macro (top one) when I defined E1 error was removed, this time I tried defining Gotit (although I would have thought the word 'string' in the '()' told isodraw it was a string.) The MESSAGE command seems to be skipped or throws same error, as the same errors occurs described previously when using MESSAGE or not.

My .txt file contains the word:

TEST

Original I know and is located:

C:\temp\Temp.txt

Its possible I am missing something as I am new to macros which may seem obvious except to a beginner. Above is all the information in the files - I have tried the Isodraw file as empty, with a text element selected. When the error occurs the contents of the text element (in this case 'TESTING') is removed.

Cheers

Alan

You were right: If a variable is passed as a parameter to a submacro,
it hasn't be defined inside. Thus you can drop the line

DEFINE Gotit AS String

The error message said IsoDraw is missing a brace, and in fact they

were missing in your IF statement. The correct syntax is

IF (condition) THEN

statements for true condition

ELSE

statements for false condition

END IF

There is another hurdle to take: If E1 is defined in the macro "Import_Text"

it it known only there (locally) and not in the submacro Get_Stuff.
To get around this, you can make E1 a global that has to be defined outside

the macros like this:


GLOBAL E1 AS ELEMENT

Here is how your file looks after theses changes:

Global E1 As Element

Macro Import_Text
E1 = ActiveDoc.FirstSelectedElement
E1.Text.String=" "
Batch "C:\temp\Temp.txt" "Get_Stuff"
End Macro

SubMacro Get_Stuff (String Gotit)
If (Gotit = " ") Then
E1.Text.String = E1.Text.String + $NewLine + $NewLine
Else
E1.Text.String = E1.Text.String + $newline + Gotit
End If
End SubMacro

Vielen Dank - works perfectly!

Top Tags