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!