Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
Hello!
I created a part window using the code:
Model tryModel = curSession.CreatePart ("Testing1");
tryModel.Display ();
Here's what happened:
Then to create the tree elements I need to access the model in the current session:
=========================================================================
Session session = pfcSession.GetCurrentSession ();
Model currModel = session.GetCurrentModel();
MessageDialogOptions msgOpts = pfcUI.MessageDialogOptions_Create();
MessageButtons buttons = MessageButtons.create();
buttons.append(MessageButton.MESSAGE_BUTTON_OK);
msgOpts.SetButtons(buttons);
msgOpts.SetMessageDialogType(MessageDialogType.MESSAGE_WARNING);
msgOpts.SetDialogLabel("OTK Sweep Feature Create ERROR");
if(currModel == null)
{
session.UIShowMessageDialog("Model must be in current Session",msgOpts);
throw new Exception("Method: pfcSession::GetCurrentModel \nMsg: otkSweepCreate");
}
=========================================================================
The condition is not met and a warning appears:
How to create a model in a tree?
Has anyone built models using OTK Java?
Help me please.
Best Regards!
Solved! Go to Solution.
I found a mistake.
Everything works now.
My code is:
public Extrude()
{
}
public void OnCommand() throws jxthrowable
{
Session session = pfcSession.GetCurrentSession ();
Model currModel = session.GetCurrentModel();
if(currModel == null)
createPart();
else
modelBuild();
}
public void createPart() throws jxthrowable {
try
{
Session curSession = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
String modelLocation = "......";
String model_name = ".......";
ModelDescriptor desc = null;
curSession.GetCurrentWindow().Activate();
desc = pfcModel.ModelDescriptor_CreateFromFileName(model_name);
desc.SetPath (modelLocation);
Model template = curSession.RetrieveModelWithOpts(desc, pfcSession.RetrieveModelOptions_Create());
Model newPart = template.CopyAndRetrieve("Test", null);
Window newWindow = curSession.CreateModelWindow(newPart);
newPart.Display();
curSession.SetCurrentWindow(newWindow);
newWindow.Activate();
template.Erase();
} catch (Exception e)
{
System.out.println("1: " + e);
Session session = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
Model currModel = session.GetModel("......", ModelType.MDL_PART);
currModel.Erase();
}
}
public void modelBuild() throws jxthrowable {
try {
int quan;
String name;
String allname = "";
Session session = pfcSession.GetCurrentSession ();
WSession ses = (WSession)session;
Model model = ses.GetCurrentModel();
Solid solid = (Solid)model;
ModelItems items = solid.ListItems(ModelItemType.ITEM_FEATURE);
for (int j = 0; j < items.getarraysize(); j++) {
name = ((Feature)items.get(j)).GetName();
quan = ((Feature)items.get(j)).ListSubItems(ModelItemType.ITEM_SURFACE).getarraysize();
allname = allname + name + ": " + quan + "\n";
}
//session.UIShowMessageDialog(allname, null);
Surface plane = (Surface)((Feature)items.get(3)).ListSubItems(ModelItemType.ITEM_SURFACE).get(0);
Surface plane1 = (Surface)((Feature)items.get(2)).ListSubItems(ModelItemType.ITEM_SURFACE).get(0);
Selection dirSels1 = pfcSelect.CreateModelItemSelection(plane, null);
Selection dirSels2 = pfcSelect.CreateModelItemSelection(plane1, null);
Elements elements = Elements.create();
//PRO_E_FEATURE_TREE
Element elem_0_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEATURE_TREE,null,0);
elements.append(elem_0_0);
//PRO_E_STD_FEATURE_NAME
Element elem_1_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_FEATURE_NAME, pfcArgument.CreateStringArgValue("My_extrude"),1);
elements.append(elem_1_0);
//PRO_E_FEATURE_FORM
Element elem_1_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEATURE_FORM, pfcArgument.CreateIntArgValue(1),1);
elements.append(elem_1_2);
//PRO_E_FEAT_FORM_IS_THIN
Element elem_1_3 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEAT_FORM_IS_THIN, pfcArgument.CreateIntArgValue(0),1);
elements.append(elem_1_3);
//PRO_E_EXT_SURF_CUT_SOLID_TYPE
Element elem_1_4 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_SURF_CUT_SOLID_TYPE, pfcArgument.CreateIntArgValue(917),1);
elements.append(elem_1_4);
//PRO_E_REMOVE_MATERIAL
Element elem_1_5 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_REMOVE_MATERIAL, pfcArgument.CreateIntArgValue(-1),1);
elements.append(elem_1_5);
//PRO_E_STD_EXT_DEPTH
Element elem_1_6 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_EXT_DEPTH, null,1);
elements.append(elem_1_6);
//PRO_E_EXT_DEPTH_FROM
Element elem_2_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM, null,2);
elements.append(elem_2_0);
//PRO_E_EXT_DEPTH_FROM_TYPE
Element elem_3_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM_TYPE, pfcArgument.CreateIntArgValue(128),3);
elements.append(elem_3_0);
//PRO_E_EXT_DEPTH_FROM_VALUE
Element elem_3_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM_VALUE, pfcArgument.CreateDoubleArgValue(100),3);
elements.append(elem_3_1);
//PRO_E_EXT_DEPTH_TO
Element elem_2_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_TO, null,2);
elements.append(elem_2_1);
//PRO_E_EXT_DEPTH_TO_TYPE
Element elem_3_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_TO_TYPE, pfcArgument.CreateIntArgValue(268435456),3);
elements.append(elem_3_2);
//PRO_E_STD_SECTION
Element elem_1_7 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SECTION, null,1);
elements.append(elem_1_7);
//PRO_E_STD_SEC_SETUP_PLANE
Element elem_2_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_SETUP_PLANE, null,2);
elements.append(elem_2_2);
//PRO_E_STD_SEC_PLANE
Element elem_3_3 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE, pfcArgument.CreateSelectionArgValue(dirSels1),3);
elements.append(elem_3_3);
//PRO_E_STD_SEC_PLANE_VIEW_DIR
Element elem_3_4 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE_VIEW_DIR, pfcArgument.CreateIntArgValue(1),3);
elements.append(elem_3_4);
//PRO_E_STD_SEC_PLANE_ORIENT_DIR
Element elem_3_5 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE_ORIENT_DIR, pfcArgument.CreateIntArgValue(1),3);
elements.append(elem_3_5);
//PRO_E_STD_SEC_PLANE_ORIENT_REF
Element elem_3_6 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE_ORIENT_REF, pfcArgument.CreateSelectionArgValue(dirSels2),3);
elements.append(elem_3_6);
WSession wSession = (WSession)session;
ElementTree elemTree = wSession.CreateElementTree(elements);
FeatCreateOptions featOpts = FeatCreateOptions.create();
featOpts.append(FeatCreateOption.FEAT_CR_INCOMPLETE_FEAT);
WRegenInstructions regenInstr = wfcSolidInstructions.WRegenInstructions_Create();
Model currModel = session.GetCurrentModel();
WSolid currSolid = (WSolid)currModel;
WFeature extrudeFeat = currSolid.WCreateFeature(elemTree,featOpts,regenInstr);
elemTree = extrudeFeat.GetElementTree(null,FeatureElemTreeExtractOptions.FEAT_EXTRACT_NO_OPTS);
Element sketchElem = GetSketcherElement(elemTree);
SpecialValue sketchSpvalue = sketchElem.GetSpecialValueElem();
Section sketch = sketchSpvalue.GetSectionValue();
sketch.SetIntentManagerMode(true);
Point2D center = wfcSession.CreatePoint2D(0.00,0.00);
SectionEntityCircle circle = wfcSection.SectionEntityCircle_Create(center,300.00);
SectionEntity entity = (SectionEntity)circle;
sketch.AddEntity(entity);
circle = wfcSection.SectionEntityCircle_Create(center,20.00);
entity = (SectionEntity)circle;
sketch.AddEntity(entity);
sketch.SetIntentManagerMode(false);
sketchSpvalue.SetSectionValue(sketch);
sketchElem.SetSpecialValueElem(sketchSpvalue);
featOpts.insert(0, FeatCreateOption.FEAT_CR_NO_OPTS);
extrudeFeat.RedefineFeature(null,elemTree,featOpts,regenInstr);
}
catch (Exception e)
{
System.out.println("1: " + e);
Session curSession = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
curSession.UIShowMessageDialog("Error!", null);
}
}
static Element GetSketcherElement(ElementTree elemTree)
{
try
{
ElemPathItems sketchItems = ElemPathItems.create();
ElemPathItem sketchItem0 = wfcElementTree.ElemPathItem_Create(ElemPathItemType.ELEM_PATH_ITEM_TYPE_ID,wfcElemIds.PRO_E_STD_SECTION);
sketchItems.append(sketchItem0);
ElemPathItem sketchItem1 = wfcElementTree.ElemPathItem_Create(ElemPathItemType.ELEM_PATH_ITEM_TYPE_ID,wfcElemIds.PRO_E_SKETCHER);
sketchItems.append(sketchItem1);
ElementPath sketchPath = wfcElementTree.ElementPath_Create(sketchItems);
Element element = elemTree.GetElement(sketchPath);
return(element);
}
catch (Exception e)
{
System.out.println("1: " + e);
}
return null;
}
Thanks everyone!
I hope it will be useful!
Hello!
I have advanced a bit. Instead of creating a model from scratch, I copied the template and then deleted the template from the session:
desc = pfcModel.ModelDescriptor_CreateFromFileName(model_name);
desc.SetPath (modelLocation);
Model template = curSession.RetrieveModelWithOpts(desc, pfcSession.RetrieveModelOptions_Create());
Model newPart = template.CopyAndRetrieve("Test", null);
template.Erase();
After that, two problems arise:
1) (First problem) I run the example "otkCreateSweep.java" immediately after creation, but the code throws the same error "Model must be in current Session". But if I first create a model with one code and run an example with the second, then everything works. I understand that the model appears in the session only after the execution of the code ends, so it is impossible to do everything right away.
So I had to do this:
static int i = 1;
public Create_Part()
{
}
@Override
public void OnCommand()
{
if (i == 1)
{createPart();
i++;}
else
addCreateSweep();
}
But it does not look like a good solution.
2) (Second problem) I am now dealing with how to make a simple extrusion in the created model. I found the code in the PTC knowledge base that allows you to unload the tree of model in XML, it seemed to me that this would be useful, but the code could not be launched.
Knowledge Base Code:
try {
Log.write("===============================================");
Log.write("=== OnAfterFeatureCreate was called =======");
Log.write("===============================================");
Session session = pfcSession.GetCurrentSession ();
wSolid = (WSolid)(session.GetCurrentModel());
if ((currModel.GetType() == ModelType.MDL_ASSEMBLY) || (currModel.GetType() == ModelType.MDL_PART)) {
try {
//wSolid = (WSolid) sld;
//cast exception com.ptc.wfc.Implementation.pfcPart$Part cannot be cast to com.ptc.wfc.wfcSolid.WSolid
wSolid = (WSolid) currModel;
wfeat = (WFeature) feat;
} catch (Throwable e) {
Log.write(" cast exceptoon : " + e);
}
ModelItem item = (ModelItem) feat;
if (item.GetType() == ModelItemType.ITEM_FEATURE) {
Log.write("GetType = ITEM_FEATURE");
Log.write("featName=" + feat.GetName());
if (wfeat != null) {
ElementTree elementtree = wfeat.GetElementTree(null,
FeatureElemTreeExtractOptions.FEAT_EXTRACT_NO_OPTS);
if (elementtree != null) {
Log.write("elementtree is not null print to XML file");
elementtree .WriteElementTreeToFile(ElementTreeFormat.ELEMTREE_XML, "feat.xml");
}
} else
{ Log.write("wfeat is null");}
}
} else
Log.write("current model is not part or assem");
} catch (Throwable e) {
System.out.println("Exception caught finial : " + e);
e.printStackTrace();
Log.write("Exception caught in Body : " + e);
}
Therefore, now I understand the documentation for Creo® Parametric TOOLKIT, it says about the element tree, but everything looks pretty confusing.
I will be grateful if someone will help me with these problems, if you have experience.
Thanks in advance.
Best Regards!
Hello!
Try to pass new model as argument to addCreateSweep (Model currModel)
Model newPart = template.CopyAndRetrieve("Test", null);
addCreateSweep(newPart);
And comment/delete a line Model currModel = session.GetCurrentModel(); in addCreateSweep ()
Thanks!
True, now the model does not produce errors, but the code still does not run 😞
Yes, I did so.
Here is my code:
Model newPart = template.CopyAndRetrieve("Test", null);
newPart.Display();
template.Erase();
addCreateSweep(newPart);
} catch (Exception e)
{
System.out.println("1: " + e);
}
}
public void addCreateSweep(Model currModel)
{
try
{
try
{
Session session = pfcSession.GetCurrentSession ();
MessageDialogOptions msgOpts = pfcUI.MessageDialogOptions_Create();
MessageButtons buttons = MessageButtons.create();
buttons.append(MessageButton.MESSAGE_BUTTON_OK);
msgOpts.SetButtons(buttons);
msgOpts.SetMessageDialogType(MessageDialogType.MESSAGE_WARNING);
msgOpts.SetDialogLabel("OTK Sweep Feature Create ERROR");
if(currModel == null)
If your program don't rise any exceptions, than try to use debugger and go thru code step by step to find a line where is a problem and to understand what happened.
Thanks!
I would like to unload the model tree in xml, and there already see the whole structure.
To do this, I need this code:
try {
Log.write("===============================================");
Log.write("=== OnAfterFeatureCreate was called =======");
Log.write("===============================================");
Session session = pfcSession.GetCurrentSession ();
wSolid = (WSolid)(session.GetCurrentModel());
if ((currModel.GetType() == ModelType.MDL_ASSEMBLY) || (currModel.GetType() == ModelType.MDL_PART)) {
try {
//wSolid = (WSolid) sld;
//cast exception com.ptc.wfc.Implementation.pfcPart$Part cannot be cast to com.ptc.wfc.wfcSolid.WSolid
wSolid = (WSolid) currModel;
wfeat = (WFeature) feat;
} catch (Throwable e) {
Log.write(" cast exceptoon : " + e);
}
ModelItem item = (ModelItem) feat;
if (item.GetType() == ModelItemType.ITEM_FEATURE) {
Log.write("GetType = ITEM_FEATURE");
Log.write("featName=" + feat.GetName());
if (wfeat != null) {
ElementTree elementtree = wfeat.GetElementTree(null,
FeatureElemTreeExtractOptions.FEAT_EXTRACT_NO_OPTS);
if (elementtree != null) {
Log.write("elementtree is not null print to XML file");
elementtree .WriteElementTreeToFile(ElementTreeFormat.ELEMTREE_XML, "feat.xml");
}
} else
{ Log.write("wfeat is null");}
}
} else
Log.write("current model is not part or assem");
} catch (Throwable e) {
System.out.println("Exception caught finial : " + e);
e.printStackTrace();
Log.write("Exception caught in Body : " + e);
}
It requires "feat" and "wfeat", which is not clear where to come from.
I tried, as in the example "otkCreateSweep.java":
WFeature sweepFeat = currSolid.WCreateFeature (elemTree, featOpts, regenInstr);
But here a tree element is created, and I need to count, so I think there should be a different logic.
Do you know anything about this?
Best Regards!
@VladiSlav wrote:
It requires "feat" and "wfeat", which is not clear where to come from.
Is Feature or ModelItem (extrusion, revolve, sweep, CSYS and so on). Is a feature for which you want to export his element tree to xml file.
You can get this feature by interactive selection or build it by your self using id and type of the feature.
Thanks, all worked well!
I shortened the code in from an example:
public void OnCommand()
{
try {
Session session = pfcSession.GetCurrentSession ();
SelectionOptions dirSelopts = pfcSelect.SelectionOptions_Create("feature, datum, csys");
dirSelopts.SetMaxNumSels(new Integer(1));
Selections dirSels = session.Select(dirSelopts, null);
ModelItem item = dirSels.get(0).GetSelItem();
Feature feat = (Feature)item;
WFeature wfeat = (WFeature) feat;
ElementTree elementtree = wfeat.GetElementTree(null, FeatureElemTreeExtractOptions.FEAT_EXTRACT_NO_OPTS);
elementtree.WriteElementTreeToFile(ElementTreeFormat.ELEMTREE_XML, "feat.xml");
} catch (Throwable e) {
e.printStackTrace();
System.out.println("????: " + e);
}
}
}
But now I have another question.
I create a plane using FET. I make a plane with an offset from the example:
The PRO_E_DTMPLN_CONSTR_REF parameter was created as follows:
//PRO_E_DTMPLN_CONSTR_REF
Element elem_3_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_DTMPLN_CONSTR_REF,pfcArgument.CreateSelectionArgValue(dirSels.get(0)),3);
elements.append(elem_3_1);
That is, I select the reference plane interactively. But I need to set the reference plane existing in the model from the code.
Do you know how to refer to the plane existing in the model?
Thanks in advance!
Best Regards!
Hi,
you must to create a Selection with ModelItem (plane) by your self.
Take a look "Programmatic Selection"
Thanks for the advice!
I wrote this code:
Session session = pfcSession.GetCurrentSession ();
WSession ses = (WSession)session;
Model model = ses.GetCurrentModel();
Solid solid = (Solid)model;
ModelItems items = solid.ListItems(ModelItemType.ITEM_SURFACE);
Surface plane = (Surface)items.get(0);
Selection dirSels1 = pfcSelect.CreateModelItemSelection(plane, null);
//PRO_E_DTMPLN_CONSTR_REF
Element elem_3_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_DTMPLN_CONSTR_REF,pfcArgument.CreateSelectionArgValue(dirSels1),3);
elements.append(elem_3_1);
It works. But only with those planes that are associated with a solid, as written in the manual:
Interface ModelItemOwner/ListItems/Parameters (Note that only those surfaces associated with solid geometry are included in the list of model items when you specify ITEM_SURFACE or null as the Type . Surfaces of Creo Parametric Surface features and Quilts are not included).
But I need to get access to any surfaces, and not just those that are associated with solids.
Do you know how to fix it?
Excuse me for asking so many questions, I started working with the API not so long ago.
Thanks in advance.
Best Regards!
@VladiSlav wrote:
But I need to get access to any surfaces, and not just those that are associated with solids.
I not sure but, I think, if you interesting to get surfaces from separate surface (feature created as surface), than need to firs get item of this feature.
ModelItems items = solid.ListItems(ModelItemType.ITEM_FEATURE);
Then take a look at chapter "Geometry Evaluation"
ps: "To obtain a list of the quilt features with their component datum surfaces, specify ITEM_QUILT."
Thanks, that helped a lot!
Now I'm dealing with extrusion.
I start, the element is created, but broken, it does not have a section.
Checking the code, I found on which line it throws an error.
My code is:
public class Extrude extends DefaultUICommandActionListener {
static int i = 0;
public Extrude()
{
}
public void OnCommand() throws jxthrowable
{
++i;
if (i == 1)
createPart();
else if (i>1)
DTM();
}
public void createPart() {
try
{
Session curSession = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
String modelLocation = ".........";
String model_name = "........";
ModelDescriptor desc = null;
curSession.GetCurrentWindow().Activate();
desc = pfcModel.ModelDescriptor_CreateFromFileName(model_name);
desc.SetPath (modelLocation);
Model template = curSession.RetrieveModelWithOpts(desc, pfcSession.RetrieveModelOptions_Create());
Model newPart = template.CopyAndRetrieve("Test", null);
Window newWindow = curSession.CreateModelWindow(newPart);
newPart.Display();
curSession.SetCurrentWindow(newWindow);
newWindow.Activate();
template.Erase();
} catch (Exception e)
{
System.out.println("1: " + e);
}
}
public void DTM() throws jxthrowable {
try {
int quan;
String name;
String allname = "";
Session session = pfcSession.GetCurrentSession ();
SelectionOptions dirSelopts = pfcSelect.SelectionOptions_Create("datum,surface,curve,csys,edge,axis");
dirSelopts.SetMaxNumSels(new Integer(1));
Selections dirSels = session.Select(dirSelopts, null);
//------------------------------------------------------------
WSession ses = (WSession)session;
Model model = ses.GetCurrentModel();
Solid solid = (Solid)model;
ModelItems items = solid.ListItems(ModelItemType.ITEM_FEATURE);
for (int j = 0; j < items.getarraysize(); j++) {
name = ((Feature)items.get(j)).GetName();
quan = ((Feature)items.get(j)).ListSubItems(ModelItemType.ITEM_SURFACE).getarraysize();
allname = allname + name + ": " + quan + "\n";
}
Surface plane = (Surface)((Feature)items.get(3)).ListSubItems(ModelItemType.ITEM_SURFACE).get(0);
Selection dirSels1 = pfcSelect.CreateModelItemSelection(plane, null);
//------------------------------------------------------------
Elements elements = Elements.create();
//PRO_E_FEATURE_TREE
Element elem_0_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEATURE_TREE,null,0);
elements.append(elem_0_0);
//PRO_E_STD_FEATURE_NAME
Element elem_1_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_FEATURE_NAME, pfcArgument.CreateStringArgValue("My_extrude"),1);
elements.append(elem_1_0);
//PRO_E_FEATURE_TYPE
Element elem_1_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEATURE_TYPE, pfcArgument.CreateIntArgValue(0),1);
elements.append(elem_1_1);
//PRO_E_FEATURE_FORM
Element elem_1_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEATURE_FORM, pfcArgument.CreateIntArgValue(1),1);
elements.append(elem_1_2);
//PRO_E_FEAT_FORM_IS_THIN
Element elem_1_3 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEAT_FORM_IS_THIN, pfcArgument.CreateIntArgValue(0),1);
elements.append(elem_1_3);
//PRO_E_EXT_SURF_CUT_SOLID_TYPE
Element elem_1_4 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_SURF_CUT_SOLID_TYPE, pfcArgument.CreateIntArgValue(917),1);
elements.append(elem_1_4);
//PRO_E_REMOVE_MATERIAL
Element elem_1_5 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_REMOVE_MATERIAL, pfcArgument.CreateIntArgValue(-1),1);
elements.append(elem_1_5);
//PRO_E_STD_EXT_DEPTH
Element elem_1_6 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_EXT_DEPTH, null,1);
elements.append(elem_1_6);
//PRO_E_EXT_DEPTH_FROM
Element elem_2_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM, null,2);
elements.append(elem_2_0);
//PRO_E_EXT_DEPTH_FROM_TYPE
Element elem_3_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM_TYPE, pfcArgument.CreateIntArgValue(128),3);
elements.append(elem_3_0);
//PRO_E_EXT_DEPTH_FROM_VALUE
Element elem_3_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM_VALUE, pfcArgument.CreateDoubleArgValue(100),3);
elements.append(elem_3_1);
//PRO_E_EXT_DEPTH_TO
Element elem_2_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_TO, null,2);
elements.append(elem_2_1);
//PRO_E_EXT_DEPTH_TO_TYPE
Element elem_3_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_TO_TYPE, pfcArgument.CreateIntArgValue(268435456),3);
elements.append(elem_3_2);
//PRO_E_STD_SECTION
Element elem_1_7 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SECTION, null,1);
elements.append(elem_1_7);
//PRO_E_STD_SEC_SETUP_PLANE
Element elem_2_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_SETUP_PLANE, null,2);
elements.append(elem_2_2);
//PRO_E_STD_SEC_PLANE
Element elem_3_3 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE, pfcArgument.CreateSelectionArgValue(dirSels1),3);
elements.append(elem_3_3);
//PRO_E_STD_SEC_PLANE_VIEW_DIR
Element elem_3_4 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE_VIEW_DIR, pfcArgument.CreateIntArgValue(1),3);
elements.append(elem_3_4);
//PRO_E_STD_SEC_PLANE_ORIENT_DIR
Element elem_3_5 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE_ORIENT_DIR, pfcArgument.CreateIntArgValue(1),3);
elements.append(elem_3_5);
WSession wSession = (WSession)session;
ElementTree elemTree = wSession.CreateElementTree(elements);
FeatCreateOptions featOpts = FeatCreateOptions.create();
featOpts.append(FeatCreateOption.FEAT_CR_INCOMPLETE_FEAT);
WRegenInstructions regenInstr = wfcSolidInstructions.WRegenInstructions_Create();
Model currModel = session.GetCurrentModel();
WSolid currSolid = (WSolid)currModel;
WFeature extrudeFeat = currSolid.WCreateFeature(elemTree,featOpts,regenInstr);
elemTree = extrudeFeat.GetElementTree(null,FeatureElemTreeExtractOptions.FEAT_EXTRACT_NO_OPTS);
Element sketchElem = GetSketcherElement(elemTree);
SpecialValue sketchSpvalue = sketchElem.GetSpecialValueElem();
Section sketch = sketchSpvalue.GetSectionValue(); //!!!!Error is here!!!!
sketch.SetIntentManagerMode(true);
Point2D center = wfcSession.CreatePoint2D(0.00,0.00);
SectionEntityCircle circle = wfcSection.SectionEntityCircle_Create(center,40.00);
SectionEntity entity = (SectionEntity)circle;
sketch.AddEntity(entity);
sketch.SetIntentManagerMode(false);
sketchSpvalue.SetSectionValue(sketch);
sketchElem.SetSpecialValueElem(sketchSpvalue);
featOpts.insert(0, FeatCreateOption.FEAT_CR_NO_OPTS);
extrudeFeat.RedefineFeature(null,elemTree,featOpts,regenInstr);
}
catch (Exception e)
{
System.out.println("1: " + e);
Session curSession = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
curSession.UIShowMessageDialog("Error!", null);
}
}
static Element GetSketcherElement(ElementTree elemTree)
{
/*
Get PRO_E_SKECTHER element using wfcElementPath_ptr
NOTE: To get an element with level greater than 1 you must
define element path with respect to PRO_E_FEATURE_TREE
*/
try
{
ElemPathItems sketchItems = ElemPathItems.create();
ElemPathItem sketchItem0 = wfcElementTree.ElemPathItem_Create(ElemPathItemType.ELEM_PATH_ITEM_TYPE_ID,wfcElemIds.PRO_E_STD_SECTION);
sketchItems.append(sketchItem0);
ElemPathItem sketchItem1 = wfcElementTree.ElemPathItem_Create(ElemPathItemType.ELEM_PATH_ITEM_TYPE_ID,wfcElemIds.PRO_E_SKETCHER);
sketchItems.append(sketchItem1);
ElementPath sketchPath = wfcElementTree.ElementPath_Create(sketchItems);
Element element = elemTree.GetElement(sketchPath);
return(element);
}
catch (Exception e)
{
System.out.println("1: " + e);
}
return null;
}
}
I created an xml file for the failed element. I attach it.
You do not know what is wrong?
Thanks in advance!
Regards!
Create feature using element tree it is not easy task. My advice is manually create Extrusion feature in Creo. Than export this feature to XML and compare it with yours XML. Maybe you miss something.
Another way is edit exported XML to meet your task and import it for creating new feature.
And do you really want to create a whole model using element tree? I think this is extremely hard. Maybe you can use "template" model. Then just change desired dimensions. Or use UDF (user defined feature).
PS: several examples of extrusion feature in C toolkit ...\Common Files\protoolkit\protk_appls\pt_userguide\ptu_featcreat
Thanks!
I have already created XML for extraction obtained in Creo. From the comparison, I concluded that my PRO_E_SKETCHER is not filled, as the error shows, as well as the created element in the model tree (there is no section there).
Now I just deal with the tree of elements, looking for possible options.
I do not want to create a model from the template, because the logic and various options make the model heavy. In my opinion, the generation will allow transferring all calculations to the program code, which will facilitate the model.
I did not know about UDF functions, thanks.
I saw examples from protoolkit, on them I also tried to make the code.
Best Regards!
I found a mistake.
Everything works now.
My code is:
public Extrude()
{
}
public void OnCommand() throws jxthrowable
{
Session session = pfcSession.GetCurrentSession ();
Model currModel = session.GetCurrentModel();
if(currModel == null)
createPart();
else
modelBuild();
}
public void createPart() throws jxthrowable {
try
{
Session curSession = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
String modelLocation = "......";
String model_name = ".......";
ModelDescriptor desc = null;
curSession.GetCurrentWindow().Activate();
desc = pfcModel.ModelDescriptor_CreateFromFileName(model_name);
desc.SetPath (modelLocation);
Model template = curSession.RetrieveModelWithOpts(desc, pfcSession.RetrieveModelOptions_Create());
Model newPart = template.CopyAndRetrieve("Test", null);
Window newWindow = curSession.CreateModelWindow(newPart);
newPart.Display();
curSession.SetCurrentWindow(newWindow);
newWindow.Activate();
template.Erase();
} catch (Exception e)
{
System.out.println("1: " + e);
Session session = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
Model currModel = session.GetModel("......", ModelType.MDL_PART);
currModel.Erase();
}
}
public void modelBuild() throws jxthrowable {
try {
int quan;
String name;
String allname = "";
Session session = pfcSession.GetCurrentSession ();
WSession ses = (WSession)session;
Model model = ses.GetCurrentModel();
Solid solid = (Solid)model;
ModelItems items = solid.ListItems(ModelItemType.ITEM_FEATURE);
for (int j = 0; j < items.getarraysize(); j++) {
name = ((Feature)items.get(j)).GetName();
quan = ((Feature)items.get(j)).ListSubItems(ModelItemType.ITEM_SURFACE).getarraysize();
allname = allname + name + ": " + quan + "\n";
}
//session.UIShowMessageDialog(allname, null);
Surface plane = (Surface)((Feature)items.get(3)).ListSubItems(ModelItemType.ITEM_SURFACE).get(0);
Surface plane1 = (Surface)((Feature)items.get(2)).ListSubItems(ModelItemType.ITEM_SURFACE).get(0);
Selection dirSels1 = pfcSelect.CreateModelItemSelection(plane, null);
Selection dirSels2 = pfcSelect.CreateModelItemSelection(plane1, null);
Elements elements = Elements.create();
//PRO_E_FEATURE_TREE
Element elem_0_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEATURE_TREE,null,0);
elements.append(elem_0_0);
//PRO_E_STD_FEATURE_NAME
Element elem_1_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_FEATURE_NAME, pfcArgument.CreateStringArgValue("My_extrude"),1);
elements.append(elem_1_0);
//PRO_E_FEATURE_FORM
Element elem_1_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEATURE_FORM, pfcArgument.CreateIntArgValue(1),1);
elements.append(elem_1_2);
//PRO_E_FEAT_FORM_IS_THIN
Element elem_1_3 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_FEAT_FORM_IS_THIN, pfcArgument.CreateIntArgValue(0),1);
elements.append(elem_1_3);
//PRO_E_EXT_SURF_CUT_SOLID_TYPE
Element elem_1_4 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_SURF_CUT_SOLID_TYPE, pfcArgument.CreateIntArgValue(917),1);
elements.append(elem_1_4);
//PRO_E_REMOVE_MATERIAL
Element elem_1_5 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_REMOVE_MATERIAL, pfcArgument.CreateIntArgValue(-1),1);
elements.append(elem_1_5);
//PRO_E_STD_EXT_DEPTH
Element elem_1_6 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_EXT_DEPTH, null,1);
elements.append(elem_1_6);
//PRO_E_EXT_DEPTH_FROM
Element elem_2_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM, null,2);
elements.append(elem_2_0);
//PRO_E_EXT_DEPTH_FROM_TYPE
Element elem_3_0 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM_TYPE, pfcArgument.CreateIntArgValue(128),3);
elements.append(elem_3_0);
//PRO_E_EXT_DEPTH_FROM_VALUE
Element elem_3_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_FROM_VALUE, pfcArgument.CreateDoubleArgValue(100),3);
elements.append(elem_3_1);
//PRO_E_EXT_DEPTH_TO
Element elem_2_1 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_TO, null,2);
elements.append(elem_2_1);
//PRO_E_EXT_DEPTH_TO_TYPE
Element elem_3_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_EXT_DEPTH_TO_TYPE, pfcArgument.CreateIntArgValue(268435456),3);
elements.append(elem_3_2);
//PRO_E_STD_SECTION
Element elem_1_7 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SECTION, null,1);
elements.append(elem_1_7);
//PRO_E_STD_SEC_SETUP_PLANE
Element elem_2_2 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_SETUP_PLANE, null,2);
elements.append(elem_2_2);
//PRO_E_STD_SEC_PLANE
Element elem_3_3 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE, pfcArgument.CreateSelectionArgValue(dirSels1),3);
elements.append(elem_3_3);
//PRO_E_STD_SEC_PLANE_VIEW_DIR
Element elem_3_4 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE_VIEW_DIR, pfcArgument.CreateIntArgValue(1),3);
elements.append(elem_3_4);
//PRO_E_STD_SEC_PLANE_ORIENT_DIR
Element elem_3_5 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE_ORIENT_DIR, pfcArgument.CreateIntArgValue(1),3);
elements.append(elem_3_5);
//PRO_E_STD_SEC_PLANE_ORIENT_REF
Element elem_3_6 = wfcElementTree.Element_Create(wfcElemIds.PRO_E_STD_SEC_PLANE_ORIENT_REF, pfcArgument.CreateSelectionArgValue(dirSels2),3);
elements.append(elem_3_6);
WSession wSession = (WSession)session;
ElementTree elemTree = wSession.CreateElementTree(elements);
FeatCreateOptions featOpts = FeatCreateOptions.create();
featOpts.append(FeatCreateOption.FEAT_CR_INCOMPLETE_FEAT);
WRegenInstructions regenInstr = wfcSolidInstructions.WRegenInstructions_Create();
Model currModel = session.GetCurrentModel();
WSolid currSolid = (WSolid)currModel;
WFeature extrudeFeat = currSolid.WCreateFeature(elemTree,featOpts,regenInstr);
elemTree = extrudeFeat.GetElementTree(null,FeatureElemTreeExtractOptions.FEAT_EXTRACT_NO_OPTS);
Element sketchElem = GetSketcherElement(elemTree);
SpecialValue sketchSpvalue = sketchElem.GetSpecialValueElem();
Section sketch = sketchSpvalue.GetSectionValue();
sketch.SetIntentManagerMode(true);
Point2D center = wfcSession.CreatePoint2D(0.00,0.00);
SectionEntityCircle circle = wfcSection.SectionEntityCircle_Create(center,300.00);
SectionEntity entity = (SectionEntity)circle;
sketch.AddEntity(entity);
circle = wfcSection.SectionEntityCircle_Create(center,20.00);
entity = (SectionEntity)circle;
sketch.AddEntity(entity);
sketch.SetIntentManagerMode(false);
sketchSpvalue.SetSectionValue(sketch);
sketchElem.SetSpecialValueElem(sketchSpvalue);
featOpts.insert(0, FeatCreateOption.FEAT_CR_NO_OPTS);
extrudeFeat.RedefineFeature(null,elemTree,featOpts,regenInstr);
}
catch (Exception e)
{
System.out.println("1: " + e);
Session curSession = pfcSession.GetCurrentSessionWithCompatibility(CreoCompatibility.C4Compatible);
curSession.UIShowMessageDialog("Error!", null);
}
}
static Element GetSketcherElement(ElementTree elemTree)
{
try
{
ElemPathItems sketchItems = ElemPathItems.create();
ElemPathItem sketchItem0 = wfcElementTree.ElemPathItem_Create(ElemPathItemType.ELEM_PATH_ITEM_TYPE_ID,wfcElemIds.PRO_E_STD_SECTION);
sketchItems.append(sketchItem0);
ElemPathItem sketchItem1 = wfcElementTree.ElemPathItem_Create(ElemPathItemType.ELEM_PATH_ITEM_TYPE_ID,wfcElemIds.PRO_E_SKETCHER);
sketchItems.append(sketchItem1);
ElementPath sketchPath = wfcElementTree.ElementPath_Create(sketchItems);
Element element = elemTree.GetElement(sketchPath);
return(element);
}
catch (Exception e)
{
System.out.println("1: " + e);
}
return null;
}
Thanks everyone!
I hope it will be useful!