Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
Todays topic is to create a coordinate system via a weblink script.
The code described here was tested under Creo 2.0 and 3.0.
A coordinate system is defined by:
Unfortunately, there is no direct way in Creo to create a coordinate system like
oSession.CurModel.CreateCsys()
To solve that, You have to use User defined features (UDFs).
I created a UDF called "wl_add_cs_w_dtms.gph.1" of a coordinate system and three corresponding datum planes.
The dimension prompts for translation are named trans_x, trans_y and trans_z. The names for rotation dimensions are set to rot_x, rot_y and rot_z.
To create Your desired csys You have to tell Creo where the UDF is stored. To do that change the corresponding config option by:
oSession.SetConfigOption("pro_group_dir", "D:\\data\\UDFs\\" );
If You have already set this config option for other purpuses, You could preveously use a
var udf_path_temp = oSession.GetConfigOptionValues("pro_group_dir");
to restore that value later.
Let's start creating those UDF-Create-Instructions.
var udf_instructions = pfcCreate ("pfcUDFCustomCreateInstructions").Create("wl_add_cs_w_dtms");
udf_instructions.DimDisplayType = pfcCreate ("pfcUDFDimensionDisplayType").UDFDISPLAY_BLANK;
The first line creates an empty instruction set and connects this to our UDF.
The second line adds the first instruction value by telling Creo that it should not display value-entering-dialogues during this script.
This defines the reference of our UDF. I use a Csys called "CS_0" in my parts as an origin.
var origin_of_csys = CurModel.GetItemByName(pfcCreate("pfcModelItemType").ITEM_COORD_SYS, "CS_0"); // this points to the reference csys
var selected_origin_of_csys = pfcCreate("MpfcSelect").CreateModelItemSelection(origin_of_csys, void null); // this selects the csys
var origin_ref = pfcCreate("pfcUDFReference").Create("UDF_REF_SYS", selected_origin_of_csys ); // this creates a reference from the selected csys
var udf_references = pfcCreate("pfcUDFReferences"); // this creates an empty container for references used by an UDF
udf_references.Append(origin_ref); // this appends our freshly created reference to this container
udf_instructions.References = udf_references; // this sets the references for our udf instruction set.
Sure, maybe You could merge all those steps to one or two lines of code. But Your code should somehow still be readable to You and other persons.
Next, We have to set the dimension values.
var udf_dimensions = pfcCreate("pfcUDFVariantValues"); // this creates an empty container for our dimension values used by the UDF
var translation_x = pfcCreate("pfcUDFVariantDimension").Create("trans_x", 10.05); // e.g. this moves the new csys 10.05mm in x - direction
udf_dimensions.Append(translation_x);
var translation_y = pfcCreate("pfcUDFVariantDimension").Create("trans_y", 5.4);
udf_dimensions.Append(translation_y);
var translation_z = pfcCreate("pfcUDFVariantDimension").Create("trans_z", 0.0);
udf_dimensions.Append(translation_z);
var rotation_x = pfcCreate("pfcUDFVariantDimension").Create("rot_x", 20);
udf_dimensions.Append(rotation_x);
var rotation_y = pfcCreate("pfcUDFVariantDimension").Create("rot_y", 30);
udf_dimensions.Append(rotation_y);
var rotation_z = pfcCreate("pfcUDFVariantDimension").Create("rot_z", 40);
udf_dimensions.Append(rotation_z);
udf_instructions.VariantValues = udf_dimensions; // this adds our dimension values to our instruction set
Almost done. One single line to create the UDF.
var udf_group = CurModel.CreateUDFGroup(udf_instructions);
At this point a new csys and three datum planes should appear in the model tree. But those features are ugly named.
I like naming a csys and its corresponding planes in a pattern like CS_<name definition>, DTM_<name definition>_XY and so on.
To rename those features, run this.
var name_def = "TEST"; // Your <name definition>
var udf_csys = CurModel.GetItemByName(pfcCreate("pfcModelItemType").ITEM_COORD_SYS, "CS_UDF_TEMP"); // this selects a feature
udf_csys.SetName("CS_" + name_def); // this renames it
var udf_dtm_xy = CurModel.GetItemByName(pfcCreate("pfcModelItemType").ITEM_FEATURE, "DTM_UDF_TEMP_XY");
udf_dtm_xy.SetName("DTM_" + name_def + "_XY");
var udf_dtm_yz = CurModel.GetItemByName(pfcCreate("pfcModelItemType").ITEM_FEATURE, "DTM_UDF_TEMP_YZ");
udf_dtm_yz.SetName("DTM_" + name_def + "_YZ");
var udf_dtm_zx = CurModel.GetItemByName(pfcCreate("pfcModelItemType").ITEM_FEATURE, "DTM_UDF_TEMP_ZX");
udf_dtm_zx.SetName("DTM_" + name_def + "_ZX");
var udf_group_feat = CurModel.GetItemByName(pfcCreate("pfcModelItemType").ITEM_FEATURE, "WL_ADD_CS_W_DTMS" );
udf_group_feat.SetName(name_def);
And finally You have to regenerate Your part. You have to have set the config option "regen_failure_handling" to "RESOLVE_MODE" to get any failures quickly fixed.
var regen_failure_handling_value = oSession.GetConfigOptionValues("regen_failure_handling"); // this stores the current value
oSession.SetConfigOption("regen_failure_handling", "RESOLVE_MODE" ); // this sets the option to "RESOLVE_MODE"
CurModel.Regenerate(void null); // this regenerates the part
oSession.SetConfigOption("regen_failure_handling", regen_failure_handling_value.Item(0) ); // this restores the old value
Done.
If You want to create a bunch of csys, it is quite helpful to pack those lines above in a single function like:
function create_csys_from_file(reference_name, name_def, trans_x, trans_y, trans_z, rot_x, rot_y, rot_z){ ... }
For those, who use Zemax (optics simulation suite) there is more to come...
Hallo Matthias, danke für den sehr brauchbaren Artikel , kannst du biite das UDF nochmals einstellen da das aktuelle nicht aufgerufen werden kann
"Retrieving group information... Can not find group."
Danke und weiter so... !!!
Das UDF in der ZIP - Datei ist korrekt (MD5 Summe stimmt mit der Quelle überein).
Prüfe, ob die config option richtig gesetzt ist.
oSession.SetConfigOption("pro_group_dir", "D:\\data\\UDFs\\" );
----------------
The attached udf-file is correct. Both files (attached udf-file and source file) have the same md5 numbers.
Please check, if config option is correct.