Jump to content

Create a duplicate of an element with the appropiate name using a script


---
 Share

Recommended Posts

Hello,

I would like to know how to create a duplicate of an element (copy and paste) and specify the name of the duplicate before it is created.

Whether you do it manually or automatically via a script, as soon as you paste the item, the program names it as “ELEMENT NAME Copy 1” or “ELEMENT NAME Copy 2” if the first name is already taken. Additionally, “Copy 1” could be “Copiar 1” or another equivalent depending on the language settings.

When you do it manually, you can easily change the name after pasting because you can see exactly what the element is. However, the problem arises when I want to automate a copy-paste operation in a script.

Currently, this is what I do:

image.png.bb8de5ee07ca215bc5a733a9f40eca45.png

As you can see, I use conditionals to obtain the word that the program uses to create duplicates (lines 1008-1019). Then, I predict the name that the software will use when the element is pasted (line 1023). After that, I perform the Copy and Paste operations (lines 1027-1031). Finally, I rename the element.

This works as long as there is no other element with the name nge_copia. That is why I wonder if there is any part of the code I could use to decide the name of the pasted element. If not, I would need a code to check if an element named nge_copia already exists.

 

Link to comment
Share on other sites

Hello,

Maybe that'll help:
When you paste items from the clipboard, it is possible to get the names of the inserted items.

elements = gom.script.sys.paste_from_clipboard (destination=[])

print(elements)

 

Link to comment
Share on other sites

Can i ask what your end goal is by copying elements in this way?  There may be other methods to acheieve your end goals.

Link to comment
Share on other sites

Thank you Marcus!

Regarding your question, James, the goal of copying elements is to use an inspection element from one item in another item with exactly the same tolerances and settings, without deleting the inspection of the initial element.

image.png.d7e7d12fa7e50eecb263f5c0d36893ce.png

For example, if I want to apply the same inspection of a cylinder to a circle using the same exact settings (as shown in the image above), my script would do this by creating a duplicate of the cylinder and assigning its diameter to the circle by editing the creation parameters.

These steps are illustrated in the image below.

image.png.2093486f672da2a4ad9467893f451f1c.png

image.png.94df23ebee975ee58f6330bd8b62797b.png

image.png.78cd1bb89607e5faff27a41a805dad18.png

image.png.c3f9b17b2a7cc88c908c5b989646a140.png

After this process, the duplicate would be erased, as it is only used as an auxiliary element.

The reason I do it this way is that it can be applied to any inspection element without having to extract all the creation parameters (which are different for each inspection element).

 

 

Link to comment
Share on other sites

Please sign in to view this quote.

I've been trying this method, but the variable "elements" I get is an array, such as these:

[gom.app.project.inspection['Cylinder 1 Copy 3'], gom.app.project.nominal_elements['Cylinder 1 Copy 3'], gom.app.project.inspection['Feature Control Frame (139) "CN027" Copy 3'], gom.app.project.inspection['Cylindrical Dimension (127) "CN028" Copy 3']]
[gom.app.project.nominal_elements['Cylinder 1 Copy 4'], gom.app.project.inspection['Cylinder 1 Copy 4'], gom.app.project.inspection['Feature Control Frame (139) "CN027" Copy 4'], gom.app.project.inspection['Cylindrical Dimension (127) "CN028" Copy 4']]

The items that appear on these lists are all the elements pasted, not only the cylinder (in this case), but also all the realated inspections.

The elements I need to track from the arrays are "gom.app.project.inspection['Cylinder 1 Copy 3']" in the first case and "gom.app.project.inspection['Cylinder 1 Copy 4']" in the second. However, these elements are not always in the same position within the array. I don’t know why this happens or what I can do to obtain the elements I want.

 

 

 

 

Link to comment
Share on other sites

There are several options for this. One of them would be:

gom.script.sys.copy_to_clipboard (elements=[nge])

gom.script.cad.edit_tags (
	add_tags=['TEST'], 
	elements=[nge])

elements = gom.script.sys.paste_from_clipboard (destination=[])

for e in elements:
	if 'TEST' in e.tags:
		print(e)

 

Link to comment
Share on other sites

Im afraid i dont quite follow...it looks like you have a goal of applying the same checks and measuring principle from say cylinder a to cylinder b ?  If thats the case id suggest looking into user defined inspection principles as these will store all relevant parameters .   I.e. Create cylinder, apply meas principle fitting cylinder , apply check a , b ,c with tolerance 1,2,3 , click on nominal cylinder and create user defined principle.   

Click on another nominal cyliner with nothing applied yet and there will be an option to apply this principle that will replay all child items stored in the principle you saved.

Does this help?

Link to comment
Share on other sites

That's right, James, my goal is to apply the same inspections from one element to another.

I know that I can access all the relevant parameters of each check in the script, but I would have to store the necessary parameters for each check to recreate the same check in another element.

For example, if we have a position inspection, I need the tolerance, the datum, material condition, etc. However, for roundness, I would only need the tolerance.

So in order to to simplify this process, I came up with the idea of duplicating “element A” (so all the inspections will be duplicated too) and then going through each check to change the element related to “element B”. This way, there is no need to obtain all the parameters of each check.

Regards,

Irune

Link to comment
Share on other sites

 Share

×
×
  • Create New...