Subscribe to this thread
Home - General / All posts - Add drawing name to table etc.
orerockon

351 post(s)
#20-Sep-17 21:40

I an batch importing hundreds of drawings that need an identifier column so I can conglomerate them into new drawings. First I need to get rid of text in the drawing name. I use the script below to remove the " Drawing" string from them. How do I replace another string in the drawing name? In my case every drawing name is prepended with the same string and I want to substitute a shorter string (same one in every drawing).

Then (ideally) I'd like to add the drawing name as a table column.

/**

* Rename Drawings

* Loops through and renames any drawing, to its existing name, minus the " Drawing"

*/

//helper functions

var alert = function(msg){Application.MessageBox(msg, "Alert"); };

var prompt = function(msg){var input = Application.InputBox(msg,"Prompt",""); return input; };

var app = Application;

var doc = Document;

var compset = doc.ComponentSet;

/**

* init

*/

function Main() {

var component;

for(var i=0;i<compset.count;i++){

component = compset.item(i);

//rename drawings with the name " Drawing"

if(component.typeName === "Drawing" &&

component.name.indexOf(" Drawing") !== -1) {

//rename component

component.name = component.name.replace(" Drawing","");

//rename owned table

component.ownedTable.name = component.ownedTable.name.replace(" Drawing","");

}

}

}

orerockon

351 post(s)
#20-Sep-17 22:09

Crap can't figure out how to delete a post. I figured out a way to rename the drawings to something I can work with. So now I'd like to add the unique part of the drawing name as a new table text column. In my case:

Drawing name = "Visit ID 96FS20112011"

For every drawing containing "Visit ID ", create and populate a text column "Visit ID" with "96FS20112011" (which is unique to each drawing name).

adamw


7,307 post(s)
#21-Sep-17 11:07

Use Table.RecordSet to access records, RecordSet methods to add a new record, Record.Data properties to set record values.

Example:

//JScript

function Main() {

  var components = Document.ComponentSet;

  var records = components.Item("IDs").RecordSet;

  for (var i = 0; i < components.Count; ++i) {

    var component = components.Item(i);

    if (component.TypeName === "Drawing" &&

        component.Name.indexOf("Visit ID ") === 0) {

      records.AddNew();

      records.LastAdded.Data("Drawing") = component.Name.substr(9);

    }

  }

}

See attached MAP file.

Attachments:
script-drawing-names-8.map

orerockon

351 post(s)
#21-Sep-17 18:09

I get an error message "Can't add table record: line 8". Is there something else I need to do with the script or the drawings to get it to work?

tjhb

7,545 post(s)
#21-Sep-17 19:43

Did you unblock the file after downloading? If not then the project may be read-only.

orerockon

351 post(s)
#21-Sep-17 20:55

I did not know that, saved it as another file reopened it and it runs. So the script populated the "IDs" drawing with a "Drawing" column that has the text of the name of each drawing that I need. How can I get that column and its value into the source drawings without making a relation?

When I wrote:

For every drawing containing "Visit ID ", create and populate a text column "Visit ID" with "96FS20112011" (which is unique to each drawing name).

I should have said:

For every drawing containing "Visit ID ", create and populate a text column "Visit ID" with "96FS20112011" in each drawing (which is unique to each drawing name).

Using the example map drawing Visit ID DEF2 needs to have a new column "Drawing" with the value "DEF2"; that column is in the "IDs" drawing.

adamw


7,307 post(s)
#22-Sep-17 07:19

OK.

Something like this?

//JScript

 

function addVisitID(drawing, value) {

  var table = drawing.OwnedTable;

 

  // add column, if not yet added

  var columns = table.ColumnSet;

  if (columns.ItemByName("Visit ID") < 0) {

    var column = columns.NewColumn();

    column.Name = "Visit ID";

    column.Type = ColumnTypeWText;

    columns.Add(column);

  }

 

  // fill column values

  var records = table.RecordSet;

  for (var i = 0; i < records.Count; ++i) {

    var record = records.Item(i);

    record.Data("Visit ID") = value;

  }

}

 

function Main() {

  var components = Document.ComponentSet;

  for (var i = 0; i < components.Count; ++i) {

    var component = components.Item(i);

    if (component.TypeName === "Drawing" &&

        component.Name.indexOf("Visit ID ") === 0)

      addVisitID(component, component.Name.substr(9));

  }

}

See attached MAP file.

Attachments:
script-drawing-names-8-2.map

orerockon

351 post(s)
#22-Sep-17 20:25

YES thanks! Do you like mead? I have a sour cherry 750 ml bottle :D

orerockon

351 post(s)
#09-Oct-17 22:42

I'm at it again, everything has been very helpful! But now I have one more question. In my example the script from Adam uses the text "Visit ID " to add a column to the table and populate it with the rest of the drawing name. I now have to import hundreds more drawings without the "Visit ID " string in the drawing name. Is there a way to prepend that to the drawing name? E.g. a drawing is named "123" and I need to prepend the string "Visit ID " so it's new name is "Visit ID 123".

steveFitz

159 post(s)
#10-Oct-17 07:31

Perhaps this would do it?

code

//Jscript

function Main() {

 var components = Document.ComponentSet;

 for (var i = 0; i < components.Count; ++i) {

 var component = components.Item(i);

 if (component.TypeName === "Drawing" &&

 component.Name.indexOf("Visit ID ") != 0) {

 component.Name = "Visit ID " + component.Name;

 }

 }

}

If so, craft beer with plenty of hops preferred 

orerockon

351 post(s)
#10-Oct-17 16:55

Wunderbar! I made a lot of beer in another life, only means and ciders the past 15 years I do enjoy one now and again and my son likes triple IPAs. I liked dunkelweizen, almost impossible to find now.

Manifold User Community Use Agreement Copyright (C) 2007-2017 Manifold Software Limited. All rights reserved.