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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

How to create Sketch Feature

KB_9753804
13-Aquamarine

How to create Sketch Feature

Hello Friends,

 

I write a function to create sketch feature , Feature created in feature navigator but 2D Line does not get added in sketch feature , I am sharing the function for your reference . Please resolve the issue.

 

ProError ProSketchFeature()
{
ProReference REPDEP_ref2;
ProReference REPDEP_ref1;
ProErrorlist errors;
ProMdl model;
ProModelitem model_item;
ProSelection model_sel;
ProFeature feature;
ProFeatureCreateOptions *opts = 0;
ProElempath path;
ProElempathItem path_items[2];
ProSection section;
ProAsmcomppath comp_path;
ProAsmcomppath *p_comp_path = NULL;
ProValue value;
double width;
double height;
double bite_radius;
double bite_height;
char name[PRO_NAME_SIZE];
ProBoolean alloc;

ProElement sketch_element;
ProElement created_elemtree;
ProElement pro_e_feature_tree;
ProElement pro_e_feature_type;
ProElement pro_e_curve_type;
ProElement pro_e_std_section;
ProElement pro_e_std_sec_method;
ProElement pro_e_std_sec_setup_plane;
ProElement pro_e_std_sec_plane;
ProElement pro_e_sketcher;
ProElement pro_e_std_sec_plane_view_dir;
ProElement pro_e_std_sec_plane_orient_dir;
ProElement pro_e_std_sec_plane_orient_ref;

ProSelection *sketch_refs;
ProName wide_string;
ProError status;
ProValueData value_data;
ProSelection * p_select;
int n_select;
ProBoolean is_interactive = PRO_B_TRUE;

/*---------------------------------------------------------------*\
Populating root element PRO_E_FEATURE_TREE
\*---------------------------------------------------------------*/
status = ProElementAlloc(PRO_E_FEATURE_TREE, &pro_e_feature_tree);
/*---------------------------------------------------------------*\
Populating element PRO_E_FEATURE_TYPE
\*---------------------------------------------------------------*/
status = ProElementAlloc(PRO_E_FEATURE_TYPE, &pro_e_feature_type);
status = ProElementIntegerSet(pro_e_feature_type, PRO_FEAT_CURVE);
status = ProElemtreeElementAdd(pro_e_feature_tree, NULL,pro_e_feature_type);

/*---------------------------------------------------------------*\
Populating element PRO_E_CURVE_TYPE
\*---------------------------------------------------------------*/
status = ProElementAlloc(PRO_E_CURVE_TYPE, &pro_e_curve_type);
status = ProElementIntegerSet(pro_e_curve_type, PRO_CURVE_TYPE_SKETCHED);
status = ProElemtreeElementAdd(pro_e_feature_tree, NULL,pro_e_curve_type);

/*---------------------------------------------------------------*\
Populating element PRO_E_STD_SECTION
\*---------------------------------------------------------------*/
status = ProElementAlloc(PRO_E_STD_SECTION, &pro_e_std_section);
status = ProElemtreeElementAdd(pro_e_feature_tree, NULL,pro_e_std_section);

/*---------------------------------------------------------------*\
Populating element PRO_E_STD_SECTION
-> PRO_E_STD_SEC_SETUP_PLANE
\*---------------------------------------------------------------*/
status = ProElementAlloc(PRO_E_STD_SEC_SETUP_PLANE,&pro_e_std_sec_setup_plane);
status = ProElemtreeElementAdd(pro_e_std_section, NULL,pro_e_std_sec_setup_plane);
sketch_refs = (ProSelection *)calloc(2, sizeof(ProSelection));

FeatByName feat_sketch[] = { { "XSEC" } };
ProSolid solid;
status = ProMdlCurrentGet((ProMdl*)&solid);
status = ProUtilFeatsByName(solid, feat_sketch, 1);
status = ProFeatureInit(solid, feat_sketch[0].id, &feature);
status = ProFeatureGeomitemVisit(&feature, PRO_TYPE_UNUSED, ProUtilFeatFirstGeomitemVisit, NULL, (ProAppData*)&model_item);

ProSurface surface;
status = ProGeomitemToSurface((ProGeomitem*)&model_item, &surface);

ProModelitem modelItem;
status = ProModelitemInit(model_item.owner, model_item.id, model_item.type, &modelItem);

int plane_id;
status = ProSurfaceIdGet(surface, &plane_id);
/*---------------------------------------------------------------*\
Populating element PRO_E_STD_SECTION
-> PRO_E_STD_SEC_SETUP_PLANE
-> PRO_E_STD_SEC_PLANE
\*---------------------------------------------------------------*/
ProSelection selection;
status = ProSelectionAlloc(NULL, &modelItem, &selection);
status = ProElementAlloc(PRO_E_STD_SEC_PLANE, &pro_e_std_sec_plane);
status = ProSelectionToReference(selection, &REPDEP_ref1);
status = ProElementReferenceSet(pro_e_std_sec_plane, REPDEP_ref1);
status = ProElemtreeElementAdd(pro_e_std_sec_setup_plane, NULL,pro_e_std_sec_plane);
/*---------------------------------------------------------------*\
Populating element PRO_E_STD_SECTION
-> PRO_E_STD_SEC_SETUP_PLANE
-> PRO_E_STD_SEC_PLANE_VIEW_DIR
\*---------------------------------------------------------------*/
status = ProElementAlloc(PRO_E_STD_SEC_PLANE_VIEW_DIR,&pro_e_std_sec_plane_view_dir);
status = ProElementIntegerSet(pro_e_std_sec_plane_view_dir, PRO_SEC_VIEW_DIR_SIDE_ONE);
status = ProElemtreeElementAdd(pro_e_std_sec_setup_plane, NULL,pro_e_std_sec_plane_view_dir);
/*---------------------------------------------------------------*\
Populating element PRO_E_STD_SECTION
-> PRO_E_STD_SEC_SETUP_PLANE
-> PRO_E_STD_SEC_PLANE_ORIENT_DIR
\*---------------------------------------------------------------*/
status = ProElementAlloc(PRO_E_STD_SEC_PLANE_ORIENT_DIR,&pro_e_std_sec_plane_orient_dir);
status = ProElementIntegerSet(pro_e_std_sec_plane_orient_dir, PRO_SEC_ORIENT_DIR_UP);
status = ProElemtreeElementAdd(pro_e_std_sec_setup_plane, NULL,pro_e_std_sec_plane_orient_dir);
/*---------------------------------------------------------------*\
Creating incomplete feature in the current model.
\*---------------------------------------------------------------*/
status = ProMdlCurrentGet(&model);
if (status != PRO_TK_NO_ERROR) return (status);
status = ProMdlToModelitem(model, &model_item);
status = ProSelectionAlloc(p_comp_path, &model_item,&model_sel);
status = ProArrayAlloc(1, sizeof(ProFeatureCreateOptions),1, (ProArray*)&opts);
opts[0] = PRO_FEAT_CR_INCOMPLETE_FEAT;
status = ProFeatureWithoptionsCreate(model_sel, pro_e_feature_tree,opts, PRO_REGEN_NO_FLAGS, &feature, &errors);
status = ProArrayFree((ProArray*)&opts);

/* Using the element tree from created feature */
status = ProFeatureElemtreeExtract(&feature, NULL, PRO_FEAT_EXTRACT_NO_OPTS, &created_elemtree);
path_items[0].type = PRO_ELEM_PATH_ITEM_TYPE_ID;
path_items[0].path_item.elem_id = PRO_E_STD_SECTION;
path_items[1].type = PRO_ELEM_PATH_ITEM_TYPE_ID;
path_items[1].path_item.elem_id = PRO_E_SKETCHER;
status = ProElempathAlloc(&path);
status = ProElempathDataSet(path, path_items, 2);
status = ProElemtreeElementGet(created_elemtree, path, &sketch_element);
status = ProElementSpecialvalueGet(sketch_element, NULL, (ProAppData *)&section);
status = ProSectionActiveSet(section);
int *int_ids, no_of_ids, i;
status = ProSectionEntityIdsGet(section, &int_ids, &no_of_ids);
status = UserCreateLine(&section);
status = ProSectionEntityIdsGet(section, &int_ids, &no_of_ids);
status = UserSetFeatureSection(&feature, &section);
return (status);
}

 

/*================================================================*\
FUNCTION : UserSetFeatureSection()
PURPOSE : Sets the section pointer to given feature and
redefine it.
\*================================================================*/
ProError UserSetFeatureSection(ProFeature* feat, ProSection *section)
{
ProError status;
ProElement elem_tree, elem;
ProElempath elem_path;
ProFeatureCreateOptions *opts = 0;
ProErrorlist err_list;
ProElempathItem sketch_path_item[] =
{
{ PRO_ELEM_PATH_ITEM_TYPE_ID, PRO_E_STD_SECTION },
{ PRO_ELEM_PATH_ITEM_TYPE_ID, PRO_E_SKETCHER }
};

status = ProFeatureElemtreeExtract(feat, NULL,PRO_FEAT_EXTRACT_NO_OPTS, &elem_tree);
status = ProElempathAlloc(&elem_path);
status = ProElempathDataSet(elem_path, sketch_path_item, 2);
status = ProElemtreeElementGet(elem_tree, elem_path, &elem);
status = ProElementSpecialvalueSet(elem, (ProAppData)*section);
status = ProElemtreeElementAdd(elem_tree, elem_path, elem);
status = ProArrayAlloc(1, sizeof(ProFeatureCreateOptions),1, (ProArray*)&opts);
opts[0] = PRO_FEAT_CR_NO_OPTS;
status = ProFeatureWithoptionsRedefine(NULL, feat, elem_tree, opts, PRO_REGEN_NO_FLAGS, &err_list);
status = ProArrayFree((ProArray*)&opts);
return status;
}

/*================================================================*\
FUNCTION : UserCreatePoint()
PURPOSE : Creates a section 2D Line in provided section.
\*================================================================*/
ProError UserCreateLine(ProSection* section)
{
Pro2dLinedef line;
ProError status;
int line_id[4];
ProName section_name;
ProWSecerror section_errors1;
ProWSecerror section_errors2;
ProWSecerror section_errors3;

int n_sec;
ProMsg error_message;
char error_message_s[100];
int i, error_id;
status = ProSecerrorAlloc(&section_errors1);
status = ProSecerrorAlloc(&section_errors2);
status = ProSecerrorAlloc(&section_errors3);
status = ProSection2DAlloc(section);

line.type = PRO_2D_LINE;
line.end1[0] = 0;
line.end1[1] = 0;
line.end2[0] = 100;
line.end2[1] = 0;
status = ProSectionEntityAdd(*section, (Pro2dEntdef*)&line, &line_id[0]);

line.type = PRO_2D_LINE;
line.end1[0] = 100;
line.end1[1] = 0;
line.end2[0] = 100;
line.end2[1] = 100;
status = ProSectionEntityAdd(*section, (Pro2dEntdef*)&line, &line_id[1]);

line.type = PRO_2D_LINE;
line.end1[0] = 100;
line.end1[1] = 100;
line.end2[0] = 0;
line.end2[1] = 100;
status = ProSectionEntityAdd(*section, (Pro2dEntdef*)&line, &line_id[2]);

line.type = PRO_2D_LINE;
line.end1[0] = 0;
line.end1[1] = 100;
line.end2[0] = 0;
line.end2[1] = 0;
status = ProSectionEntityAdd(*section, (Pro2dEntdef*)&line, &line_id[3]);

status = ProSectionEpsilonSet(*section, 0.1);
status = ProSectionAutodim(*section, &section_errors1);

if (status != PRO_TK_NO_ERROR)
{
status = ProSecerrorCount(&section_errors1, &n_sec);
for (i = 0; i < n_sec; i++)
{
status = ProSecerrorMsgGet(section_errors1, i, error_message);
ProWstringToString(error_message_s, error_message);
status = ProSecerrorItemGet(section_errors1, i, &error_id);
}
}
status = ProSectionRegenerate(*section, &section_errors3);
if (status != PRO_TK_NO_ERROR)
{
status = ProSecerrorCount(&section_errors3, &n_sec);
}
return (status);
}

 

 

 

1 REPLY 1

Your PRO_E_STD_SECTION element of the feature tree is missing the PRO_E_STD_SEC_PLANE_ORIENT_REF element.

 

Next, in your UserCreateLine function you don't create any reference to the existing geometry. that means that your 0,0 coordinates are not defined and your sketched lines may not be placed in the desired location.

 

Check your documentation, especially the section called "Element Trees: Sketched Features" in the user guide.

Also there are some good examples in <creo_toolkit_loadpoint>/protk_appls/pt_userguide/ptu_featcreat. Have a look at UgSketchedCurveCreate.c and Ug3DSection.c. Those two file are showing an example of creating a sketched curve.

 

One last thing. The way you posted the code example makes it really hard to read. For the future I strongly suggest to use indentations to make it more easy to read.

Something like:

if (status != PRO_TK_NO_ERROR)
{
	status = ProSecerrorCount(&section_errors1, &n_sec);
	for (i = 0; i < n_sec; i++)
	{
		status = ProSecerrorMsgGet(section_errors1, i, error_message);
		ProWstringToString(error_message_s, error_message);
		status = ProSecerrorItemGet(section_errors1, i, &error_id);
	}
}

compared to:


status = ProSectionAutodim(*section, &section_errors1);

if (status != PRO_TK_NO_ERROR)
{
status = ProSecerrorCount(&section_errors1, &n_sec);
for (i = 0; i < n_sec; i++)
{
status = ProSecerrorMsgGet(section_errors1, i, error_message);
ProWstringToString(error_message_s, error_message);
status = ProSecerrorItemGet(section_errors1, i, &error_id);
}
}


 

Announcements


Top Tags