This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

DEVELOPERS FORUM - Copy of all related item

Xavier Bertschy - Thursday, September 11, 2014 3:53 AM:

Hello,

I am new in Aras and I would like to be able to do a "Save As" which makes a copy of the selected Item and all the related Items. For instance, I have an assembly named "Assy" which has 3 related components, when I do a "Save As" on "Assy" Aras only makes a copy of the "Assy" Item but not of the 3 related elements. Moreover, when a part is related to a document, this should also be duplicated. Would it be possible to create a new Actions that makes possible the creation of a copy for all related items?

I tried by writing a Javascript method on client-side but I am not a programming expert. I succeeded to print a list of items from the Part ItemType (thanks to the programmers guide), but not to make a copy of all the listed elements and their related document.

Thanks in advance for any help :)

 



Xavier Bertschy - Monday, September 15, 2014 4:57 AM:

Hello,

I succeeded to make a copy of all the elements involved. Nevertheless, I am still unable to set the relation between the different parts (i.e. when I open the assembly, the part BOM tab doesn't show the parts which it has relation with. Here is my current Method:

 

//Set up the query Item
var qryItem = this.newItem("Part","get");
qryItem.setAttribute("select","name, test, description");
qryItem.setID(this.getID());


//Set up the BOM structure
var bomItem = this.newItem("Part BOM","get");
bomItem.setAttribute("select","quantity,related_id(test,name,description)");
qryItem.addRelationship(bomItem);

var results = qryItem.apply();

if (results.isError()) {
    top.aras.AlertError("Item not found");
    return;
}

var bomItems = results.getRelationships( );
var count = bomItems.getItemCount();

if (count != 0)
{
    //New Assy
    var innovator = new Innovator();
    var new_assy = innovator.newItem("Part","add");
   
    var new_assy_name =results.getProperty("name");
    var new_assy_descri = results.getProperty("description");
    var new_assy_choix_numbering = results.getProperty("list_numbering");
    var new_assy_class = results.getProperty("classification");
   
    new_assy.setProperty("name","copy of " + new_assy_name);
    new_assy.setProperty("description",new_assy_descri);
    new_assy.setProperty("list_numbering",new_assy_choix_numbering);
    new_assy.setProperty("classification",classification);

    new_assy = new_assy.apply();

//Tableau pour les résultats
var content = "<table border='1'>" +
    "<tr>" +
    "<td>ID</td>" +
    "<td>Part Number</td>" +
    "<td>Name</td>" +
    "<td>Description</td> " +
    "<td>Quantity</td>" +
    "</tr>";

//Liste des items dans la BOM et création des nouveaux items
for (var i=0; i<count; ++i)
{
    var bom = bomItems.getItemByIndex(i);
    var bomPart = bom.getRelatedItem();
   

var innovator = new Innovator();

var nouveau_item = innovator.newItem("Part","add");
       
var nouveau_nom = bomPart.getProperty("name");
var nouveau_description = bomPart.getProperty("description");
var nouveau_cost = bomPart.getProperty("cost");
var nouveau_class = bomPart.getProperty("classification");
var nouveau_choix_numbering = bomPart.getProperty("list_numbering");
var nouveau_bom_qty = bom.getProperty("quantity");
   
nouveau_item.setProperty("name","copy of " + nouveau_nom);
nouveau_item.setProperty("description",nouveau_description);
nouveau_item.setProperty("classification",nouveau_class);
nouveau_item.setProperty("cost",nouveau_cost);
nouveau_item.setProperty("list_numbering",nouveau_choix_numbering);
   
nouveau_item.setProperty("quantity",nouveau_bom_qty);
//   
    content += "<tr>" +
        "<td>" + bomPart.getID() + "</td>" +
        "<td>" + bomPart.getProperty("test") + "</td>" +
        "<td>" + bomPart.getProperty("name") + "</td>" +
        "<td>" + bomPart.getProperty("description") + "</td>" +
        "<td>" + bom.getProperty("quantity") + "</td>" +
        "</tr>";
   
    nouveau_item = nouveau_item.apply();
}

    return content +"</table>";

}    else {
    top.aras.AlertError("The selected part has no child");
    return;
}

Moreover, I also would like to copy the related CAD file of every part, thus I added the followin code, but I get an error:

//Set up the file
var CAD_file = this.newItem("Part CAD","get");
CAD_file.setAtttribute("select","native_file");
qryItem.addRelationship(CAD_file);

Any contribution would be a big plus! Thank you :)





DavidSpackman - Monday, September 15, 2014 10:07 PM:

HI Xavier, 

 

Did you see this Aras project? http://www.aras.com/projects/project-view.aspx?id=602D18E572DB447985FBDC06061565F9

It should be of help

 

Dave



Xavier Bertschy - Tuesday, September 16, 2014 3:10 AM:

Hello David,

Thanks, I will investigate in this way!

EDIT: I successfully wrote a method which copies the selected Part and all their childs and make also a copy on the vault of all the related CAD documents. If anyone is interested, you can send me an PM or reply to this thread.

There is only one think that my method does not do: it does not keep the BOM structure. I would like to re-create all the links bewteen the parts and the CAD documents as they were created. Indeed, once my parts and CAD are copied, they are no longer linked between each other, but I have no idea how to do that. Does anyone know how could I do that?

Moreover, the script only work when there is 1 level of the BOM structure, if a child has a child, it is not took in account.

Many thanks!

Xavier



Xavier Bertschy - Wednesday, September 17, 2014 4:01 AM:

Hello again,

I have successfully wrote a method wich copies the selected Part plus all their direct child (and all the related CAD documents) but not the childs of the childs. Indeed I would like to copy all the BOM structure from the parent I selected. Below is my method (C#), does anyone know what should I add in order to take into account all the level of the BOM?

Many thanks!


Innovator innovator = this.getInnovator();

if (this.getType() != "Part")
{
    return innovator.newError("Item must be of type Part");
}

//Set up the query Item
Item qryItem = this.newItem("Part","get");
qryItem.setAttribute("select","name,test,description,cost,classification,list_numbering");
qryItem.setID(this.getID());

//Set up the CAD Doc
Item cadDoc = this.newItem("Part CAD","get");
cadDoc.setAttribute("select","related_id");
qryItem.addRelationship(cadDoc);

Item results = qryItem.apply();

//##########  NEW ASSY ##########
Item new_assy = innovator.newItem("Part","add");

    string new_assy_name = results.getProperty("name");
    string new_assy_descri = results.getProperty("description");
    string new_assy_choix_numbering = results.getProperty("list_numbering");
    string new_assy_class = results.getProperty("classification");
   
    new_assy.setProperty("name","copy of " + new_assy_name);
    new_assy.setProperty("description",new_assy_descri);
    new_assy.setProperty("list_numbering",new_assy_choix_numbering);
    new_assy.setProperty("classification",new_assy_class);

new_assy = new_assy.apply();

string source_ID = new_assy.getID();

Item bomcads = results.getRelationships();
int count = bomcads.getItemCount();
int i;

//List and copy all CAD Doc related to the assembly
for (i=0; i<count; ++i)
{
    Item bom = bomcads.getItemByIndex(i);
    Item bomCAD = bom.getRelatedItem();
   
    Innovator inn = this.getInnovator();
    string docID = bomCAD.getID();

//Perform the copyAsNew on the CAD document creating the new CAD Document and
//referenced file (native_file)
Item copyDoc = inn.newItem("CAD","copyAsNew");
copyDoc.setID(docID);
Item newDoc = copyDoc.apply();
   
    string CAD_id = newDoc.getID();
   
    Item relatedCAD_Assy = innovator.newItem("Part CAD","add");
    relatedCAD_Assy.setProperty("source_id",source_ID);
    relatedCAD_Assy.setProperty("related_id",CAD_id);
    relatedCAD_Assy = relatedCAD_Assy.apply();
   
if (newDoc.isError())
{
    return newDoc;
}
string newDocClass = newDoc.getProperty("classification","");
newDocClass = newDocClass.Replace("Copy of ","");
newDoc.setProperty("classification",newDocClass);

//set is_current=1 (handles older generations of File)
inn.applySQL("UPDATE [CAD] SET is_current = '1', classification= '"+newDocClass+"' WHERE [id]='"+newDoc.getID()+"'");

//Fix the Copied CAD Document (create new native file, set viewable file to
//null, and unlock)
Item fixNewDoc = inn.newItem("CAD", "edit");
fixNewDoc.setID(newDoc.getID());
fixNewDoc.setAttribute("version","0");
   
//Make a copy of the native_file and set
string natID = newDoc.getProperty("native_file","");
if (natID !="")
{
    Item newNat = inn.newItem("file","copyAsNew");
    newNat.setID(natID);
    newNat=newNat.apply();
    fixNewDoc.setPropertyItem("native_file",newNat);
}
//set viewable_file to null and unlock with Edit
fixNewDoc.setProperty("viewable_file","");
newDoc=fixNewDoc.apply();
}


//####################  COPY THE SELECTED ASSEMBLY AND ALL RELATED ITEMS ###################
//Initialize qryItem
qryItem.removeRelationship(cadDoc);

//Set up BOM structure
Item bomItem = this.newItem("Part BOM","get");
bomItem.setAttribute("select", "quantity,related_id(name,test,description,cost,classification,list_numbering)");
qryItem.addRelationship(bomItem);

results = qryItem.apply();

Item bomItems = results.getRelationships();
count = bomItems.getItemCount();


string content ="<table border='1'>" +
    "<tr>" +
    "<td>ID</td>" +
    "<td>Part Number</td>" +
    "<td>Name</td>" +
    "<td>Description</td> " +
    "<td>Classification</td>" +
    "<td>Choix du numbering</td>" +
    "<td>Quantity</td>" +
    "<td>Native File</td>" +
    "</tr>";

for (i=0; i<count; i++)
{

    Item bom = bomItems.getItemByIndex(i);
    Item bomPart = bom.getRelatedItem();
   
    //########## NEW PARTS ##############
    Item nouveau_item = innovator.newItem("Part","add");
   
    string nouveau_nom = bomPart.getProperty("name");
    string nouveau_description = bomPart.getProperty("description");
    string nouveau_cost = bomPart.getProperty("cost");
    string nouveau_class = bomPart.getProperty("classification");
    string nouveau_choix_numbering = bomPart.getProperty("list_numbering");
    string nouveau_bom_qty = bom.getProperty("quantity");
   
    nouveau_item.setProperty("name","copy of " + nouveau_nom);
    nouveau_item.setProperty("description",nouveau_description);
    nouveau_item.setProperty("classification",nouveau_class);
    nouveau_item.setProperty("cost",nouveau_cost);
    nouveau_item.setProperty("list_numbering",nouveau_choix_numbering);
   
        content += "<tr>" +
        "<td>" + bomPart.getID() + "</td>" +
        "<td>" + bomPart.getProperty("test") + "</td>" +
        "<td>" + bomPart.getProperty("name") + "</td>" +
        "<td>" + bomPart.getProperty("description") + "</td>" +
        "<td>" + bomPart.getProperty("classification") + "</td>" +
        "<td>" + bomPart.getProperty("list_numbering") + "</td>" +
        "<td>" + bom.getProperty("quantity") + "</td>" +
        "</tr>";
   

    nouveau_item = nouveau_item.apply();
    string related_ID = nouveau_item.getID();
   
        Item relatedItem = innovator.newItem("Part BOM","add");
        relatedItem.setProperty("source_id",source_ID);
        relatedItem.setProperty("related_id",related_ID);
        relatedItem = relatedItem.apply();

    //Set up the query part
Item qryPart = this.newItem("Part","get");
qryPart.setAttribute("select","name");
qryPart.setID(bomPart.getID());

//Set up the CAD Doc
Item cadDocPart = this.newItem("Part CAD","get");
cadDocPart.setAttribute("select","related_id");
qryPart.addRelationship(cadDocPart);

Item resultsPart = qryPart.apply();

Item bomcadsPart = resultsPart.getRelationships();
int count_cad = bomcadsPart.getItemCount();
int j;

//List and copy all CAD Doc related to the part i
for (j=0; j<count_cad; ++j)
{
    bom = bomcadsPart.getItemByIndex(j);
    Item bomCADPart = bom.getRelatedItem();
   
    Innovator inn = this.getInnovator();
    string docID = bomCADPart.getID();

//Perform the copyAsNew on the CAD document creating the new CAD Document and
//referenced file (native_file)
Item copyDoc = inn.newItem("CAD","copyAsNew");
copyDoc.setID(docID);
Item newDoc = copyDoc.apply();
   
        string CAD_ID = newDoc.getID();
   
    Item relatedCAD = innovator.newItem("Part CAD","add");
    relatedCAD.setProperty("source_id",related_ID);
    relatedCAD.setProperty("related_id",CAD_ID);
    relatedCAD = relatedCAD.apply();

if (newDoc.isError())
{
    return newDoc;
}
string newDocClass = newDoc.getProperty("classification","");
newDocClass = newDocClass.Replace("Copy of ","");
newDoc.setProperty("classification",newDocClass);

//set is_current=1 (handles older generations of File)
inn.applySQL("UPDATE [CAD] SET is_current = '1', classification= '"+newDocClass+"' WHERE [id]='"+newDoc.getID()+"'");

//Fix the Copied CAD Document (create new native file, set viewable file to
//null, and unlock)
Item fixNewDoc = inn.newItem("CAD", "edit");
fixNewDoc.setID(newDoc.getID());
fixNewDoc.setAttribute("version","0");

//Make a copy of the native_file and set
string natID = newDoc.getProperty("native_file","");
if (natID !="")
{
    Item newNat = inn.newItem("file","copyAsNew");
    newNat.setID(natID);
    newNat=newNat.apply();
    fixNewDoc.setPropertyItem("native_file",newNat);
}
//set viewable_file to null and unlock with Edit
fixNewDoc.setProperty("viewable_file","");
newDoc=fixNewDoc.apply();
   
   
}

}
content += "</table>";

return innovator.newResult(content);


PS: I am not an expert so the method is certainly a bit slow.

 



DavidSpackman - Wednesday, September 17, 2014 7:54 PM:

Hi Xavier, 

 

Have a look at the stored procedure called MultiBom_GetCompleteBom1. It will return a list of all parts in a multilevel BOM structure.

Have a look at the method PE_GetMultilBom to see how you can call it.

 

I would also consider how many of parts you actually want to create copies of the information for. 

i.e. What about off the shelf parts you use in your designs, do you need to create copies of that information?

 

Dave



Xavier Bertschy - Monday, September 22, 2014 8:22 AM:

Hi,

Thank you, the method you are talking about are a bit obscure to me. But I will try to use a recursive funtion to achieve my goal. If anyone is interested I'll let you know when I am done.

Thanks a lot!

Xavier



zahar - Monday, September 22, 2014 12:23 PM:

Xavier,

I will suggest to try following approach for this task (it's not so different from yours but it can solve you the recursion):

 

  1. Create server method that:
    1. will run on specific Part item and get it's BOM: 
      <Item type='Part BOM' action='get' where="[Part_BOM].source_id = '{SOURCE_ID}'" select='related_id,source_id'/>
      replace {SOURCE_ID} with this.getID() if you writing C# code 
    2. select all related_id from the result
    3. create copy of child by creating new aml for each related_id and run it: 
      <Item type='Part' action='copy' id='{RELATED_ID}' /> 
    4. each aml will return ID of new copy and you need to run for it following AML:
       <Item type='Part BOM' action='add'>
      <source_id>{SOURCE_ID}'</source_id>
      <related_id>{NEW_ID of PART}</related_id>
      </Item> 
    5. remove old BOM by creating new aml for each related_id and run it: 
      <Item type='Part BOM' action='delete' WHERE="[Part_BOM].source_id = '{SOURCE_ID}' and [Part_BOM].related_id in({LIST OF RELATED_ID splited by comma})" />  
  2.     Attach this method to OnAfterCopy event of Part item type 

 

So you will get recursion by default without need to create your own. I have split the idea of the method to different AML's but you can do some of them in one AML

 

 



Xavier Bertschy - Tuesday, September 23, 2014 6:36 AM:

Thanks Zahar for your detailed answer, I actually already wrote my method with a recursive function and everything works fine!

But thanks, your approach seems very intersting, I wasn't aware of the OnAfterCopy event. Nevertheless, I don't understand what is created the recursion in your method? Is it the OnAfterCopy event?

Xavier

PS: if anyone is intersted in the script that duplicate a complete BOM structure and their related CAD files, feel free to email me.

 



zahar - Monday, September 29, 2014 6:30 PM:

Hi Xavier,

what is created the recursion in my method is not the method but the way Aras working. Every time you execute AML with "copy" action on related item Aras will fire the OnAfterCopy method for this related item.