LIST_OF_ENTITIES * STEPWrapper::getListOfEntities(int STEPid, const char *name) { LIST_OF_ENTITIES *l = new LIST_OF_ENTITIES; SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); sse->ResetAttributes(); STEPattribute *attr; while ((attr = sse->NextAttribute()) != NULL) { std::string attrval; std::string attrname = attr->Name(); if (attrname.compare(name) == 0) { STEPaggregate *sa = (STEPaggregate *)attr->ptr.a; EntityNode *sn = (EntityNode *)sa->GetHead(); SDAI_Application_instance *se; while (sn != NULL) { se = (SDAI_Application_instance *)sn->node; l->push_back(se); sn = (EntityNode *)sn->NextNode(); } break; } } return l; }
SDAI_Application_instance * STEPWrapper::getEntity(int STEPid, const char *name) { SDAI_Application_instance *se = getEntity(STEPid); if (se->IsComplex()) { se = getSuperType(STEPid, name); } return se; }
void STEPWrapper::getSuperTypes(int STEPid, MAP_OF_SUPERTYPES &m) { SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); if (sse->IsComplex()) { STEPcomplex *sc = ((STEPcomplex *)sse)->head; while (sc) { m[sc->EntityName()] = sc; sc = sc->sc; } } }
MAP_OF_SUPERTYPES * STEPWrapper::getMapOfSuperTypes(int STEPid) { MAP_OF_SUPERTYPES *m = new MAP_OF_SUPERTYPES; SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); if (sse->IsComplex()) { STEPcomplex *sc = ((STEPcomplex *)sse)->head; while (sc) { (*m)[sc->EntityName()] = sc; sc = sc->sc; } } return m; }
LIST_OF_STRINGS * STEPWrapper::getAttributes(int STEPid) { LIST_OF_STRINGS *l = new LIST_OF_STRINGS; SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); sse->ResetAttributes(); STEPattribute *attr; while ((attr = sse->NextAttribute()) != NULL) { std::string name = attr->Name(); l->push_back(name); } return l; }
SDAI_Application_instance * STEPWrapper::getSuperType(int STEPid, const char *name) { SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); std::string attrval; if (sse->IsComplex()) { STEPcomplex *sc = ((STEPcomplex *)sse)->head; while (sc) { std::string ename = sc->EntityName(); if (ename.compare(name) == 0) { return sc; } sc = sc->sc; } } return NULL; }
LIST_OF_LIST_OF_POINTS * STEPWrapper::getListOfListOfPoints(int STEPid, const char *attrName) { LIST_OF_LIST_OF_POINTS *l = new LIST_OF_LIST_OF_POINTS; SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); sse->ResetAttributes(); STEPattribute *attr; while ((attr = sse->NextAttribute()) != NULL) { std::string attrval; std::string name = attr->Name(); if (name.compare(attrName) == 0) { ErrorDescriptor errdesc; //std::cout << attr->asStr(attrval) << std::endl; //std::cout << attr->TypeName() << std::endl; GenericAggregate_ptr gp = (GenericAggregate_ptr)attr->ptr.a; STEPnode *sn = (STEPnode *)gp->GetHead(); //EntityAggregate *ag = new EntityAggregate(); const char *eaStr; LIST_OF_POINTS *points; while (sn != NULL) { //sn->STEPwrite(std::cout); //std::cout << std::endl; eaStr = sn->asStr(attrval); points = parseListOfPointEntities(eaStr); l->push_back(points); sn = (STEPnode *)sn->NextNode(); } break; } } return l; }
double STEPWrapper::getRealAttribute(int STEPid, const char *name) { double retValue = 0.0; SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); sse->ResetAttributes(); STEPattribute *attr; while ((attr = sse->NextAttribute()) != NULL) { std::string attrname = attr->Name(); if (attrname.compare(name) == 0) { retValue = *attr->ptr.r; break; } } return retValue; }
std::string STEPWrapper::getStringAttribute(int STEPid, const char *name) { std::string retValue = ""; SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); sse->ResetAttributes(); STEPattribute *attr; while ((attr = sse->NextAttribute()) != NULL) { std::string attrval; std::string attrname = attr->Name(); if (attrname.compare(name) == 0) { retValue = attr->asStr(attrval); //if (retValue.empty()) // std::cout << "String retValue:" << retValue << ":" << std::endl; break; } } return retValue; }
Logical STEPWrapper::getLogicalAttribute(int STEPid, const char *name) { Logical retValue = LUnknown; SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); sse->ResetAttributes(); STEPattribute *attr; while ((attr = sse->NextAttribute()) != NULL) { std::string attrname = attr->Name(); if (attrname.compare(name) == 0) { retValue = (Logical)(*attr->ptr.e).asInt(); if (retValue > LUnknown) { retValue = LUnknown; } break; } } return retValue; }
Boolean STEPWrapper::getBooleanAttribute(int STEPid, const char *name) { Boolean retValue = BUnset; SDAI_Application_instance *sse = instance_list->FindFileId(STEPid)->GetSTEPentity(); sse->ResetAttributes(); STEPattribute *attr; while ((attr = sse->NextAttribute()) != NULL) { std::string attrname = attr->Name(); if (attrname.compare(name) == 0) { retValue = (Boolean)(*attr->ptr.e).asInt(); if (retValue > BUnset) { retValue = BUnset; } break; } } return retValue; }
bool STEPWrapper::convert(BRLCADWrapper *dot_g) { MAP_OF_PRODUCT_NAME_TO_ENTITY_ID name2id_map; MAP_OF_ENTITY_ID_TO_PRODUCT_NAME id2name_map; MAP_OF_ENTITY_ID_TO_PRODUCT_ID id2productid_map; MAP_OF_PRODUCT_NAME_TO_ENTITY_ID::iterator niter = name2id_map.end(); if (!dot_g) { return false; } this->dotg = dot_g; int num_ents = instance_list->InstanceCount(); for (int i = 0; i < num_ents; i++) { SDAI_Application_instance *sse = instance_list->GetSTEPentity(i); if (sse == NULL) { continue; } std::string name = sse->EntityName(); std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))std::tolower); if ((sse->STEPfile_id > 0) && (sse->IsA(SCHEMA_NAMESPACE::e_shape_definition_representation))) { ShapeDefinitionRepresentation *sdr = dynamic_cast<ShapeDefinitionRepresentation *>(Factory::CreateObject(this, (SDAI_Application_instance *)sse)); if (!sdr) { bu_exit(1, "ERROR: unable to allocate a 'ShapeDefinitionRepresentation' entity\n"); } else { int sdr_id = sdr->GetId(); std::string pname = sdr->GetProductName(); int product_id = sdr->GetProductId(); id2productid_map[sdr_id] = product_id; if (pname.empty()) { std::string str = "ShapeDefinitionRepresentation@"; str = dotg->GetBRLCADName(str); id2name_map[sdr_id] = pname; } else { std::string temp = pname; int index = 2; while ((niter=name2id_map.find(temp)) != name2id_map.end()) { temp = pname + "_" + static_cast<ostringstream*>( &(ostringstream() << (index++)) )->str(); } pname = temp; if ((niter=name2id_map.find(pname)) == name2id_map.end()) { id2name_map[sdr_id] = pname; name2id_map[pname] = product_id; id2name_map[product_id] = pname; } } AdvancedBrepShapeRepresentation *aBrep = sdr->GetAdvancedBrepShapeRepresentation(); if (aBrep) { if (pname.empty()) { std::string str = "product@"; pname = dotg->GetBRLCADName(str); id2name_map[aBrep->GetId()] = pname; id2name_map[product_id] = pname; } else { id2name_map[aBrep->GetId()] = pname; id2name_map[product_id] = pname; } id2productid_map[aBrep->GetId()] = product_id; if (Verbose()) { if (!pname.empty()) { std::cerr << std::endl << " Generating Product -" << pname ; } else { std::cerr << std::endl << " Generating Product"; } } LocalUnits::length = aBrep->GetLengthConversionFactor(); LocalUnits::planeangle = aBrep->GetPlaneAngleConversionFactor(); LocalUnits::solidangle = aBrep->GetSolidAngleConversionFactor(); ON_Brep *onBrep = aBrep->GetONBrep(); if (!onBrep) { delete sdr; bu_exit(1, "ERROR: failure creating advanced boundary representation from %s\n", stepfile.c_str()); } else { ON_TextLog tl; if (!onBrep->IsValid(&tl)) { bu_log("WARNING: %s is not valid\n", name.c_str()); } //onBrep->SpSplitClosedFaces(); //ON_Brep *tbrep = TightenBrep(onBrep); mat_t mat; MAT_IDN(mat); Axis2Placement3D *axis = aBrep->GetAxis2Placement3d(); if (axis != NULL) { //assign matrix values double translate_to[3]; const double *toXaxis = axis->GetXAxis(); const double *toYaxis = axis->GetYAxis(); const double *toZaxis = axis->GetZAxis(); mat_t rot_mat; VMOVE(translate_to,axis->GetOrigin()); VSCALE(translate_to,translate_to,LocalUnits::length); MAT_IDN(rot_mat); VMOVE(&rot_mat[0], toXaxis); VMOVE(&rot_mat[4], toYaxis); VMOVE(&rot_mat[8], toZaxis); bn_mat_inv(mat, rot_mat); MAT_DELTAS_VEC(mat, translate_to); } dotg->WriteBrep(pname, onBrep,mat); delete onBrep; } } else { // must be an assembly if (pname.empty()) { std::string str = "assembly@"; pname = dotg->GetBRLCADName(str); } ShapeRepresentation *aSR = sdr->GetShapeRepresentation(); if (aSR) { int sr_id = aSR->GetId(); id2name_map[sr_id] = pname; id2name_map[product_id] = pname; id2productid_map[sr_id] = product_id; } } Factory::DeleteObjects(); } } } /* * Pickup BREP related to SHAPE_REPRESENTATION through SHAPE_REPRESENTATION_RELATIONSHIP * * like the following found in OpenBook Part 'C': * #21281=SHAPE_DEFINITION_REPRESENTATION(#21280,#21270); * #21280=PRODUCT_DEFINITION_SHAPE('','SHAPE FOR C.',#21279); * #21279=PRODUCT_DEFINITION('design','',#21278,#21275); * #21278=PRODUCT_DEFINITION_FORMATION_WITH_SPECIFIED_SOURCE('1','LAST_VERSION',#21277,.MADE.); * #21277=PRODUCT('C','C','NOT SPECIFIED',(#21276)); * #21270=SHAPE_REPRESENTATION('',(#21259),#21267); * #21259=AXIS2_PLACEMENT_3D('DANTE_BX_CPU_TOP_1',#21256,#21257,#21258); * #21267=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#21266)) * GLOBAL_UNIT_ASSIGNED_CONTEXT((#21260,#21264,#21265))REPRESENTATION_CONTEXT('ID1','3')); * * #21271=SHAPE_REPRESENTATION_RELATIONSHIP('','',#21270,#21268); * #21268=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#21254),#21267); * #21272=SHAPE_REPRESENTATION_RELATIONSHIP('','',#21270,#21269); * #21269=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('',(#21255),#21267); * */ for (int i = 0; i < num_ents; i++) { SDAI_Application_instance *sse = instance_list->GetSTEPentity(i); if (sse == NULL) { continue; } std::string name = sse->EntityName(); std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))std::tolower); if ((sse->STEPfile_id > 0) && (sse->IsA(SCHEMA_NAMESPACE::e_shape_representation_relationship))) { ShapeRepresentationRelationship *srr = dynamic_cast<ShapeRepresentationRelationship *>(Factory::CreateObject(this, (SDAI_Application_instance *)sse)); if (srr) { ShapeRepresentation *aSR = dynamic_cast<ShapeRepresentation *>(srr->GetRepresentationRelationshipRep_1()); AdvancedBrepShapeRepresentation *aBrep = dynamic_cast<AdvancedBrepShapeRepresentation *>(srr->GetRepresentationRelationshipRep_2()); if (!aBrep) { //try rep_1 aBrep = dynamic_cast<AdvancedBrepShapeRepresentation *>(srr->GetRepresentationRelationshipRep_1()); aSR = dynamic_cast<ShapeRepresentation *>(srr->GetRepresentationRelationshipRep_2()); } if ((aSR) && (aBrep)) { int sr_id = aSR->GetId(); MAP_OF_ENTITY_ID_TO_PRODUCT_ID::iterator it = id2productid_map.find(sr_id); if (it != id2productid_map.end()) { // product found int product_id = (*it).second; int brep_id = aBrep->GetId(); it = id2productid_map.find(brep_id); if (it == id2productid_map.end()) { // brep not loaded yet so lets do that here. string pname = id2name_map[product_id]; id2productid_map[brep_id] = product_id; if (Verbose()) { if (!pname.empty()) { std::cerr << std::endl << " Generating Product -" << pname ; } else { std::cerr << std::endl << " Generating Product"; } } LocalUnits::length = aBrep->GetLengthConversionFactor(); LocalUnits::planeangle = aBrep->GetPlaneAngleConversionFactor(); LocalUnits::solidangle = aBrep->GetSolidAngleConversionFactor(); ON_Brep *onBrep = aBrep->GetONBrep(); if (!onBrep) { bu_exit(1, "ERROR: failure creating advanced boundary representation from %s\n", stepfile.c_str()); } else { ON_TextLog tl; if (!onBrep->IsValid(&tl)) { bu_log("WARNING: %s is not valid\n", name.c_str()); } //onBrep->SpSplitClosedFaces(); //ON_Brep *tbrep = TightenBrep(onBrep); mat_t mat; MAT_IDN(mat); Axis2Placement3D *axis = aBrep->GetAxis2Placement3d(); if (axis != NULL) { //assign matrix values double translate_to[3]; const double *toXaxis = axis->GetXAxis(); const double *toYaxis = axis->GetYAxis(); const double *toZaxis = axis->GetZAxis(); mat_t rot_mat; VMOVE(translate_to,axis->GetOrigin()); VSCALE(translate_to,translate_to,LocalUnits::length); MAT_IDN(rot_mat); VMOVE(&rot_mat[0], toXaxis); VMOVE(&rot_mat[4], toYaxis); VMOVE(&rot_mat[8], toZaxis); bn_mat_inv(mat, rot_mat); MAT_DELTAS_VEC(mat, translate_to); } dotg->WriteBrep(pname, onBrep,mat); delete onBrep; } } } } Factory::DeleteObjects(); } } } if (Verbose()) { std::cerr << std::endl << " Generating BRL-CAD hierarchy." << std::endl; } for (int i = 0; i < num_ents; i++) { SDAI_Application_instance *sse = instance_list->GetSTEPentity(i); if (sse == NULL) { continue; } std::string name = sse->EntityName(); std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))std::tolower); if ((sse->STEPfile_id > 0) && (sse->IsA(SCHEMA_NAMESPACE::e_context_dependent_shape_representation))) { ContextDependentShapeRepresentation *aCDSR = dynamic_cast<ContextDependentShapeRepresentation *>(Factory::CreateObject(this, (SDAI_Application_instance *)sse)); if (aCDSR) { int rep_1_id = aCDSR->GetRepresentationRelationshipRep_1()->GetId(); int rep_2_id = aCDSR->GetRepresentationRelationshipRep_2()->GetId(); int pid_1 = id2productid_map[rep_1_id]; int pid_2 = id2productid_map[rep_2_id]; Axis2Placement3D *axis1 = NULL; Axis2Placement3D *axis2 = NULL; if ((id2name_map.find(rep_1_id) != id2name_map.end()) && (id2name_map.find(rep_2_id) != id2name_map.end())) { string comb = id2name_map[rep_1_id]; string member = id2name_map[rep_2_id]; mat_t mat; MAT_IDN(mat); ProductDefinition *relatingProduct = aCDSR->GetRelatingProductDefinition(); ProductDefinition *relatedProduct = aCDSR->GetRelatedProductDefinition(); if (relatingProduct && relatedProduct) { string relatingName = relatingProduct->GetProductName(); int relatingID = relatingProduct->GetProductId(); string relatedName = relatedProduct->GetProductName(); int relatedID = relatedProduct->GetProductId(); if ((relatingID == pid_1) && (relatedID == pid_2)) { axis1 = aCDSR->GetTransformItem_1(); axis2 = aCDSR->GetTransformItem_2(); comb = id2name_map[rep_1_id]; member = id2name_map[rep_2_id]; } else if ((relatingID == pid_2) && (relatedID == pid_1)) { axis1 = aCDSR->GetTransformItem_2(); axis2 = aCDSR->GetTransformItem_1(); comb = id2name_map[rep_2_id]; member = id2name_map[rep_1_id]; } else { std::cerr << "Error: Found Representation Relationship Rep_1(name=" << comb << ",Id=" << rep_1_id << ")" << std::endl; std::cerr << "Error: Found Representation Relationship Rep_2(name=" << member << ",Id=" << rep_2_id << ")" << std::endl; std::cerr << "Error: but Relating ProductDefinition (name=" << relatingName << ",Id=" << relatingID << ")" << std::endl; std::cerr << "Error: Related ProductDefinition (name=" << relatedName << ",Id=" << relatedID << ")" << std::endl; } } if ((axis1 != NULL) && (axis2 != NULL)) { mat_t to_mat; mat_t from_mat; mat_t toinv_mat; //assign matrix values double translate_to[3]; double translate_from[3]; const double *toXaxis = axis1->GetXAxis(); const double *toYaxis = axis1->GetYAxis(); const double *toZaxis = axis1->GetZAxis(); const double *fromXaxis = axis2->GetXAxis(); const double *fromYaxis = axis2->GetYAxis(); const double *fromZaxis = axis2->GetZAxis(); VMOVE(translate_to,axis1->GetOrigin()); VSCALE(translate_to,translate_to,LocalUnits::length); VMOVE(translate_from,axis2->GetOrigin()); VSCALE(translate_from,translate_from,-LocalUnits::length); // undo from trans/rot MAT_IDN(from_mat); VMOVE(&from_mat[0], fromXaxis); VMOVE(&from_mat[4], fromYaxis); VMOVE(&from_mat[8], fromZaxis); MAT_DELTAS_VEC(from_mat, translate_from); // do to trans/rot MAT_IDN(to_mat); VMOVE(&to_mat[0], toXaxis); VMOVE(&to_mat[4], toYaxis); VMOVE(&to_mat[8], toZaxis); bn_mat_inv(toinv_mat, to_mat); MAT_DELTAS_VEC(toinv_mat, translate_to); bn_mat_mul(mat, toinv_mat, from_mat); } dotg->AddMember(comb,member,mat); } Factory::DeleteObjects(); } } } if (!dotg->WriteCombs()) { std::cerr << "Error writing BRL-CAD hierarchy." << std::endl; } return true; }
bool Representation::Load(STEPWrapper *sw, SDAI_Application_instance *sse) { step = sw; id = sse->STEPfile_id; // need to do this for local attributes to makes sure we have // the actual entity and not a complex/supertype parent sse = step->getEntity(sse, ENTITYNAME); name = step->getStringAttribute(sse, "name"); if (items.empty()) { LIST_OF_ENTITIES *l = step->getListOfEntities(sse, "items"); LIST_OF_ENTITIES::iterator i; for (i = l->begin(); i != l->end(); i++) { SDAI_Application_instance *entity = (*i); if (entity) { RepresentationItem *aRI = dynamic_cast<RepresentationItem *>(Factory::CreateObject(sw, entity)); if (aRI != NULL) items.push_back(aRI); } else { std::cerr << CLASSNAME << ": Unhandled entity in attribute 'items'." << std::endl; l->clear(); delete l; return false; } } l->clear(); delete l; } if (context_of_items.empty()) { SDAI_Application_instance *entity = step->getEntityAttribute(sse, "context_of_items"); if (entity) { if (entity->IsComplex()) { SDAI_Application_instance *sub_entity = step->getEntity(entity, "Geometric_Representation_Context"); if (sub_entity) { GeometricRepresentationContext *aGRC = new GeometricRepresentationContext(); context_of_items.push_back(aGRC); if (!aGRC->Load(step, sub_entity)) { std::cout << CLASSNAME << ":Error loading GeometricRepresentationContext" << std::endl; return false; } } sub_entity = step->getEntity(entity, "Global_Uncertainty_Assigned_Context"); if (sub_entity) { GlobalUncertaintyAssignedContext *aGUAC = new GlobalUncertaintyAssignedContext(); context_of_items.push_back(aGUAC); if (!aGUAC->Load(step, sub_entity)) { std::cout << CLASSNAME << ":Error loading GlobalUncertaintyAssignedContext" << std::endl; return false; } } sub_entity = step->getEntity(entity, "Global_Unit_Assigned_Context"); if (sub_entity) { GlobalUnitAssignedContext *aGUAC = new GlobalUnitAssignedContext(); context_of_items.push_back(aGUAC); if (!aGUAC->Load(step, sub_entity)) { std::cout << CLASSNAME << ":Error loading GlobalUnitAssignedContext" << std::endl; return false; } } sub_entity = step->getEntity(entity, "Parametric_Representation_Context"); if (sub_entity) { ParametricRepresentationContext *aPRC = new ParametricRepresentationContext(); context_of_items.push_back(aPRC); if (!aPRC->Load(step, sub_entity)) { std::cout << CLASSNAME << ":Error loading ParametricRepresentationContext" << std::endl; return false; } } } else { RepresentationContext *aRC = dynamic_cast<RepresentationContext *>(Factory::CreateObject(sw, entity)); if (aRC != NULL) { context_of_items.push_back(aRC); } else { std::cout << CLASSNAME << ":Error loading RepresentationContext" << std::endl; return false; } } } else { std::cout << CLASSNAME << ":Error loading \"context_of_items\"" << std::endl; return false; } } return true; }