hexagon logo

my solution to .CSV output

Since it has come up a few times I thought I would share my solution to outputting data to a .csv file. I originally wrote this in VB express but I have converted it and tested it in 3.7's basic script editor and it works.

The output will be formatted as such: Dimension name, feature name, measured axis, nominal, + tolerance, - tolerance, measurement.

LOC1,LIN1,Y,-0.415,0.5,0.5,-0.40984622932167

Any other work that I need to do with the data I do in Excel after I import the file.

The data will output to the c:\ in a file named PCD Output.csv but with a little tweaking you can change this as you like.

I have done a lot more with this in VBE, but as far as the basics go it get's the job done.


There is a function call in the program but that makes it too long for a single post so you will need to get that from the next post.
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub main[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      'Delare variable for file name to write to[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim strFileName As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      'connect to PCD program[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim ObjApp As Object[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim ObjPP As Object[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim ObjCmds As Object[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim ObjCmd As Object[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim ObjPart As Object[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim lngNumCmds As Long[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Set ObjApp = CreateObject("PCDLRN.Application")[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Set ObjPart = ObjApp.ActivePartProgram[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Set ObjCmds = ObjPart.Commands[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      set lngNumCmds = ObjCmds.Count[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      'Declare variables used for gather output info[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim StrDimID As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim StrDimFeature As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim StrDimType As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim StrDimNominal As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim StrDimUTol As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim StrDimLTol As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim StrDimMeasure As String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      'Declare variable for creating CSV text like[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Dim StrOutputLine As String[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]              'Build and assign file name[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      strFileName = "C:\PCD output.txt"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Open strFileName For Output As #1[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      For Each ObjCmd In ObjCmds[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]          If ObjCmd.IsDimension Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              If Not (ObjCmd.DimensionCommand.IsLocationAxis Or ObjCmd.DimensionCommand.IsTruePosAxis) Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]                  StrDimID = ObjCmd.DimensionCommand.ID[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]                  StrDimFeature = ObjCmd.DimensionCommand.Feat1[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              End If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              StrDimType = FncCmdType(ObjCmd.Type)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              StrDimNominal = CStr(ObjCmd.DimensionCommand.Nominal)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              StrDimUTol = CStr(ObjCmd.DimensionCommand.Plus)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              StrDimLTol = CStr(ObjCmd.DimensionCommand.Minus)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              If StrDimType <> "TPDM" Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]                  StrDimMeasure = CStr(ObjCmd.DimensionCommand.Measured)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              Else[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]                  StrDimMeasure = CStr(ObjCmd.DimensionCommand.Deviation)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              End If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              If Not (StrDimType = "Start_Loc" Or StrDimType = "Start_TP") Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]                  StrOutputLine = StrDimID + "," + StrDimFeature + "," + StrDimType + "," + StrDimNominal + "," + StrDimUTol + "," + StrDimLTol + "," + StrDimMeasure[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]                  print #1, StrOutputLine[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]              End If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]          End If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      Close #1[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]      MsgBox("File save complete")[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End Sub[/COLOR][/SIZE]
[/COLOR][/SIZE]
  • Jim,

    I know that this isn't exactly your code but it's something I did for somewhere else to test a problem getting and setting variables.

    [COLOR=#0000ff]Dim[/COLOR][SIZE=2] ObjApp [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ObjPP [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ObjCmds [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ObjCmd [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ObjPart [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] lngNumCmds [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Long[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ObjApp = CreateObject("PCDLRN.Application")[/SIZE]
    [SIZE=2]Set ObjPart = ObjApp.ActivePartProgram[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ObjCmds = ObjPart.Commands[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] lngNumCmds = ObjCmds.Count[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Var [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Var = objPart.GetVariableValue ("TESTVAR")[/SIZE]
    [SIZE=2]Var.Stringvalue = "OH Happy Day"[/SIZE]
    [SIZE=2]ObjPart.SetVariableValue "TESTVAR", Var[/SIZE]


    The only thing that I see right off that is drasticly different is that the GetVariableValue method is looking to contain the variable name in parantheses. Try to change your commands to this and see if it gets rid of your error.
    Set txtPartName = objPart.GetVariableValue [COLOR=red]([/COLOR]"V1[COLOR=red]")[/COLOR]
    Set txtSerialNumber = objPart.GetVariableValue [COLOR=red]([/COLOR]"V1"[COLOR=red])[/COLOR]


    As for setting the directory path there are a few ways to go about this. My original code had a static path and file name set
    [SIZE=2][COLOR=#0000ff]strFileName = "C:\PCD output.txt" [COLOR=black]
    [/COLOR][/COLOR][/SIZE]

    vpt added a dynamic method that goes to the program path back in post #3

    [COLOR=#0000ff]strFileName = ObjApp.ActivePartProgram.Path & "PCD output.txt" [/COLOR][COLOR=black]
    [/COLOR]


    To put it all together with what you have going on would be something like
    [COLOR=#0000ff]strFileName = ObjApp.ActivePartProgram.Path & strFile [/COLOR][COLOR=black]
    [/COLOR]


    right after the code snippet you have posted here. There are other ways to dynamically change the path it's all going to depend on what you are trying to accomplish though. I have found, as I am sure that you have, that everyone's file structure is different and it's a matter of figuring out what works for the file structure you have to work with.
  • You could also incorporate the code for a savedialog, prompting the user for the destination.
  • For the newbies, File/Export/Generic will get you what you want. Or a text save from Datapage. This tip is for scripters to automate their customized outputs.
  • Brian, I tried your suggestion and this is what happened.

    DIM LOC1= TRUE POSITION OF CIRCLE CIR1 UNITS=MM ,$
    GRAPH=OFF TEXT=OFF MULT=10.00 OUTPUT=REPORT FIT TO DATUMS=ON DEV PERPEN CENTERLINE=OFF DISPLAY=DIAMETER
    AX MEAS NOMINAL +TOL -TOL BONUS DEV OUTTOL
    X 203.0589 203.0000 0.0589
    Y 25.3906 25.4000 -0.0094
    DF 25.4062 25.4000 0.0100 0.0100 0.0038 0.0062 0.0000
    TP MMC 0.0500 0.0038 0.1194 0.0656
    END OF DIMENSION LOC1

    DIM LOC2= TRUE POSITION OF CIRCLE CIR2 UNITS=MM ,$
    GRAPH=OFF TEXT=OFF MULT=10.00 OUTPUT=REPORT FIT TO DATUMS=ON DEV PERPEN CENTERLINE=OFF DISPLAY=DIAMETER
    AX MEAS NOMINAL +TOL -TOL BONUS DEV OUTTOL
    X 266.5421 266.5000 0.0421
    Y 76.1238 76.2000 -0.0762
    DF 19.0557 19.0500 0.0100 0.0100 0.0043 0.0057 0.0000
    TP MMC 0.0500 0.0043 0.1742 0.1198
    END OF DIMENSION LOC2

    LOC1 AX:X, 203.000000, 0.000000, 0.000000, 0.000000, 203.058945, 0.058945, 0.000000, 0.000000
    LOC1 AX:Y, 25.400000, 0.000000, 0.000000, 0.000000, 25.390598, -0.009402, 0.000000, 0.000000
    LOC1 AXSmileyF, 25.400000, 0.010000, 0.010000, 0.000000, 25.406205, 0.006205, 0.000000, 0.000000
    LOC1 AX:TP MOD: RFS-RFS, 0.000000, 0.050000, 0.000000, 0.000000, 0.000000, 0.119380, -9.062978, 0.065585

    LOC2 AX:X, 266.500000, 0.000000, 0.000000, 0.000000, 266.542070, 0.042070, 0.000000, 0.000000
    LOC2 AX:Y, 76.200000, 0.000000, 0.000000, 0.000000, 76.123756, -0.076244, 0.000000, 0.000000
    LOC2 AXSmileyF, 19.050000, 0.010000, 0.010000, 0.000000, 19.055651, 0.005651, 0.000000, 0.000000
    LOC2 AX:TP MOD: RFS-RFS, 0.000000, 0.050000, 0.000000, 0.000000, 0.000000, 0.174161, -61.110821, 0.119812

    Please explain what the red area means.

    Thank you in advance.
  • That'd be my guess as well. Throw a legacy TP in there or add it to a label to correlate.

    DEVANG perhaps? (Deviation angle)
  • This is legacy dimension output.

    Here is the output using the csv script.


    LOC1,CIR1,TPX,203,0,0,203.0589448595
    LOC1,CIR1,TPY,25.4,0,0,25.390597623407
    LOC1,CIR1,TPDF,25.4,0.01,0.01,25.406204570924
    LOC1,CIR1,TPDM,0,0.05,0,0.11938008455356

    LOC2,CIR2,TPX,266.5,0,0,266.54207017692
    LOC2,CIR2,TPY,76.2,0,0,76.123755975445
    LOC2,CIR2,TPDF,19.05,0.01,0.01,19.055650730872
    LOC2,CIR2,TPDM,0,0.05,0,0.17416143162304
  • I changed the format statement to include devang and it is the devang, but I didn't specify it in the previous dimension output.
  • Generic output doesn't give you control over what is output. It is a basic dumping of the dimension info into a CSV file. Fast and quick for new users to get data into Excel without using the wizard which gives you more control, or scripting for even more control.

    I changed the format statement to include devang and it is the devang, but I didn't specify it in the previous dimension output.
  • but if you open the file for Append it would add new data to the end of the old. Not an ideal solution but at least you have the old data still.[/COLOR][/COLOR][/SIZE]


    Thanks very much for the code, but could you explain how to open the file to Append it? I would like to have it just add the data everytime instead of overwriting it.

    Thanks very much for your time and help.