cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Generating a model with Java OTK_model tree

SOLVED
Pearl

Generating a model with Java OTK_model tree

Hello!

 

I created a part window using the code:

      Model tryModel = curSession.CreatePart ("Testing1");
      tryModel.Display ();

Here's what happened:

Снимок1.PNG

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:
Снимок2.PNG

How to create a model in a tree?
Has anyone built models using OTK Java?
Help me please.

 

Best Regards!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: Generating a model with Java OTK_model tree

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!

 

View solution in original post

17 REPLIES 17
Highlighted

Re: Generating a model with Java OTK_model tree

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!

Highlighted

1

 
Highlighted

2

 
Tags (1)
Highlighted

Re: 1

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 ()

 

Highlighted

Re: Generating a model with Java OTK_model tree

Thanks!

True, now the model does not produce errors, but the code still does not run 😞

Highlighted

Re: Generating a model with Java OTK_model tree

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)

 

 

Highlighted

Re: Generating a model with Java OTK_model tree

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.

Highlighted

Re: Generating a model with Java OTK_model tree

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!

Highlighted

Re: Generating a model with Java OTK_model tree

@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.

Announcements
Business Continuity with Creo: Learn more about it here.