/* Course MEEM5990 Design Automation: Theory and Implementation Mechanical Design Research Lab Mechanical Engineering - Engineering Mechanics Dept. Michigan Technological University Copyright (C) 2001. All Rights Reserved. FILE: feature_controller.cc MODULE: none REQUIRES LIBRARIES: libXm, libXt, libX11 + Orbix and OI libraries AUTHOR(S): Bernie Bettig OVERVIEW ======== This program presents three sliders that can be used to control the position and size (radius) of the hole feature in the part "intface.mf1". (This file must be opened before the program is started.) The program gets a pointer to the part, then to the history tree, then to the root node and finally to the hole feature node. The position of the hole is controlled by specifying the transformation matrix and the radius by specifying the parameter. Code from the I-DEAS Open Architecture examples has been used liberally. Note that there is almost no error checking in this code. See the I-DEAS Open Architecture examples to see how proper error checking is done. */ #include "controls_main.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct mydata { CORBA::Boolean connected; // Connected to Open I-DEAS Server OI_Event *events; // event handler OI_Server_ptr oiServer; // Open I-DEAS Server OI_CommandServer_ptr command; // Open I-DEAS Command Server OI_AccessControlServer_ptr accessControl; // Open I-DEAS Access Control }; static void myConnect(mydata *); static void myCleanup(mydata *); static void myErrorSent(OI_Base_ptr , void *, const OI_EventData &); static void myMessageSent(OI_Base_ptr , void *, const OI_EventData &); class FeatureControlTool : public sh_Tool { public: FeatureControlTool(OI_Part_ptr apart, OI_FeatureNode_ptr afeature, OI_Dimension_ptr arad_var); sh_Control *CreateFeatureControl(sh_CompositeControl *parent); void ControlEvent(sh_Control *, sh_Event &e); protected: OI_FeatureNode_ptr feature; OI_Part_ptr part; OI_Dimension_ptr rad_var; double translation_X, translation_Z, hole_rad; }; class CloseEventHandler : public sh_Tool { public: CloseEventHandler(mydata *aasy) { asy = aasy; } void ControlEvent(sh_Control *, sh_Event &e) { if (!strcmp(e.GetMessage(), "AboutToCloseProgram")) myCleanup(asy); } protected: mydata *asy; }; int main() { OI_ErrorCode status; mydata asy; sh_MainWindow *main_win = new sh_MainWindow("Feature Control", new CloseEventHandler(&asy)); myConnect(&asy); // must come after new sh_MainWindow() OI_BinSequence_var bins = asy.oiServer->GetBins("Main", status); OI_Bin_ptr mainBin = bins[0]; OI_PartSequence_var parts = mainBin->GetParts("*", "*", "*", OID_GET_LATEST_VERSION, status); OI_Part_ptr part = parts[0]; OI_HistoryTree_ptr tree = part->GetHistoryTree(); OI_FeatureNode_ptr root = tree->GetRootNode(); OI_FeatureNode_ptr hole = root->GetRightChild(status); OI_DimensionSequence_var dims = hole->GetAllKeyDimensions(status); OI_Dimension_ptr rad = dims[0]; FeatureControlTool *featureControlTool = new FeatureControlTool(part, hole, rad); featureControlTool->CreateFeatureControl(main_win); main_win->Run(); return 0; // this line is never reached } FeatureControlTool::FeatureControlTool(OI_Part_ptr apart, OI_FeatureNode_ptr afeature, OI_Dimension_ptr arad) { part = apart; feature = afeature; rad_var = arad; translation_X = 0.5; translation_Z = 0.5; hole_rad = 0.5; } sh_Control *FeatureControlTool::CreateFeatureControl(sh_CompositeControl *parent) { sh_Form *form = new sh_Form(parent); sh_Control *c1 = new sh_Label(form, "-50 X pos 50"); c1->IsBelow(NULL); sh_Control *c2 = new sh_Slider(form, &translation_X, False, True, this); c2->IsBelow(c1); c2->IsRightOf(NULL, 20); sh_Control *c3 = new sh_Label(form, "-50 Z pos 50"); c3->IsBelow(c2); sh_Control *c4 = new sh_Slider(form, &translation_Z, False, True, this); c4->IsBelow(c3); c4->IsRightOf(NULL, 20); sh_Control *c5 = new sh_Label(form, "0.1 rad 20"); c5->IsBelow(c4); c5->IsRightOf(NULL, 10); sh_Control *c6 = new sh_Slider(form, &hole_rad, False, True, this); c6->IsBelow(c5); c6->IsRightOf(NULL, 20); c6->IsAbove(NULL); return form; } void FeatureControlTool::ControlEvent(sh_Control *, sh_Event &e) { if (!strcmp(e.GetMessage(), "SliderSlid")) { OI_ErrorCode status; OI_Transform transform; transform[0][0] = transform[1][1] = transform[2][2] = 1.; transform[0][1] = transform[1][0] = 0.; transform[0][2] = transform[2][0] = 0.; transform[1][2] = transform[2][1] = 0.; transform[3][1] = 0.; transform[3][0] = (translation_X-.5)*.1; transform[3][2] = (translation_Z-.5)*.1; feature->SetRelativeTransform(transform); rad_var->ModifyValue(hole_rad*0.02+0.0001); part->Update(True); } } static void myConnect(mydata *asy) { int argc = 0; char *argv[] = {""}; asy->connected = FALSE; try // This is a C++ keyword for error handling. { // get the server object asy->oiServer = OI_Connect(argc, argv); asy->connected = TRUE; // get a copy of I-DEAS command server object asy->command = asy->oiServer->GetCommandServer(); // only allow us to control I-DEAS asy->accessControl = asy->oiServer->GetAccessControlServer(); asy->accessControl->LockServer(); asy->command->DisableOutput(); // register callbacks for events in which interested asy->events = new OI_Event_X(sh_MainWindow::GetMainWindow()->GetAppContext(), asy->oiServer); asy->events->AddCallback(OIE_SendList, (OI_EventCallback)myMessageSent, asy, FALSE); asy->events->AddCallback(OIE_SendError, (OI_EventCallback)myErrorSent, asy, TRUE); } // handle any exceptions that were thrown catch (CORBA::SystemException se) { asy->connected = FALSE; myCleanup(asy); cout << "CORBA Failure" << se << endl; exit(EXIT_FAILURE); } catch (...) { myCleanup(asy); cout << "Unexpected exception in OI Server Conection and Setup" << endl; exit(EXIT_FAILURE); } } /* NAME * myCleanup * * DESCRIPTION * this utility cleans up memory, disconects from OI_Server, etc. */ static void myCleanup(mydata *asy) { try { if (!CORBA::is_nil(asy->accessControl)) CORBA::release(asy->accessControl); if (!CORBA::is_nil(asy->command)) CORBA::release(asy->command); } catch(CORBA::SystemException& se) { cout << "CORBA Failure" << &se << endl; } catch(...) { cout << "Unexpected exception in OI Server Conection and Setup" << endl; } asy->oiServer = NULL; asy->command = NULL; asy->accessControl = NULL; } /* NAME * myErrorSent * * DESCRIPTION * This function is called when Error was sent from ideas */ static void myErrorSent(OI_Base_ptr baseObject, void *clientData, const OI_EventData &eventData) { mydata *asy = (mydata *)clientData; OI_ErrorCode status; try { eventData.anyData >>= status; cout << (char *)asy->oiServer->GetErrorMessage(status) << endl; } catch(CORBA::SystemException& se) { cout << "CORBA Failure" << &se << endl; } catch(...) { cout << "Unexpected exception in OI Server Conection and Setup" << endl; } } /* NAME * myMessageSent * * DESCRIPTION * This function is called when message was sent from ideas */ static void myMessageSent(OI_Base_ptr baseObject, void *clientData, const OI_EventData &eventData) { char *result; eventData.anyData >>= result; cout << result << endl; }