The main purpose is to go through all child elements of a STEP model and to make a tree view out of them using OpenCascade. Now I am downloading the STEP model according to a particular path to a TopoDS_Shape object and then I am passing this object to the AIS_Shape object in order to finally display the model in the viewport. So, I thought that there was a method that should get a model path or a model itself as a parameter, then recursively go through all its child and finaly prints them somewhere
void OcctGtkViewer::onSTEPLoad(std::string filename)
{
string tempFileName = "";
STEPControl_Reader reader;
TopoDS_Shape shape;
if (filename == "")
filename = tempFileName;
const char *encodename = filename.c_str();
if (reader.ReadFile(encodename) != IFSelect_RetDone)
{
cout << "Выбран не STEP файл!" << endl;
return;
}
Standard_Integer nbr = reader.NbRootsForTransfer();
for (Standard_Integer n = 1; n <= nbr; n++)
{
cout << "STEP: передаю корневой объект " << n << endl;
reader.TransferRoot(n);
}
Standard_Integer nbs = reader.NbShapes();
shape = reader.OneShape();
STEPShape = reader.OneShape();
cout << "STEP: файл загружен " << endl;
//TDocStd_Document document("/home/kirill/Desktop/1.STEP");
//traverseDocument(document);
return;
}
{
// dummy shape for testing
onSTEPLoad(pathToFile);
// TopoDS_Shape aBox = BRepPrimAPI_MakeBox(100.0, 50.0, 90.0).Shape();
Handle(AIS_Shape) aShape = new AIS_Shape(STEPShape);
myContext->Display(aShape, AIS_Shaded, 0, false);
}
This might be a very small issue, but I'm really wondering why it is not working. I'm trying to output the facet ID associated to every halfedge.
void MeshModel::printFacetsOfHalfedges() {
for (Polyhedron::Halfedge_iterator j = P_.halfedges_begin(); j != P_.halfedges_end(); ++j) {
int id = j->facet()->id();
std::cout << "Facet is: " << id << std::endl;
}
}
It starts with Facet is: 0 and then crashes. If I remove the line std::cout << "Facet is: " << id << std::endl; the iteration runs just fine. I'm really wondering how this simple int output messes up the code.
I'm aware that I could also iterate over facets (and this works fine), but I need the halfedge<->facet association.
I'm using CGAL::Polyhedron_items_with_id_3 and initialize my facet IDs at the beginning:
void MeshModel::initializeFacetIndices() {
std::size_t i = 0;
for (Polyhedron::Facet_iterator facet = P_.facets_begin(); facet != P_.facets_end(); ++facet) {
facet->id() = i++;
}
}
You need to check if the halfedge is not a boundary halfedge. In such a case j->facet() == Polyhedron::Face_handle()
First, I am new to C++; now,I am trying to write a project to listen to any database change from MySQL binlog and do something. I chose a library called mysql-binlog-repliation, following its example codes, now I can track event in MySQL binlog, retrieve old values and new values. However, I can not retrieve columns, I want to know how to retrieve columns together with values, appreciate for any useful answers!
Below is part of my codes:
while (true) {
int result = binlog.wait_for_next_event(&event);
if (result == ERR_EOF)
break;
std::cout << "Found event of type " << event->get_event_type();
int event_type = event->get_event_type();
if (event_type == mysql::TABLE_MAP_EVENT) {
tmev = (mysql::Table_map_event *)event;
std::string tablename=tmev->table_name;
}
if (event_type == mysql::WRITE_ROWS_EVENT || event_type==mysql::UPDATE_ROWS_EVENT || event_type==mysql::DELETE_ROWS_EVENT) {
mysql::Row_event *row_event = (mysql::Row_event *)event;
mysql::Row_event_set rows(row_event, tmev);
mysql::Row_event_set::iterator itor = rows.begin();
do {
mysql::Row_of_fields fields = *itor;
mysql::Row_of_fields::iterator it = fields.begin();
// Here,fields represent values,I have no way to get columns
do {
std::string out;
converter.to(out, *it);
std::cout << "\t" << out;
} while (++it != fields.end());
} while (++itor != rows.end());
}
std::cout << std::endl;
int gas;
// Input Code
int user_code;
std::cout << std::endl;
std::cout << "Please enter the Code: ";
std::cin >> user_code;
std::cout << "The value you entered is " << user_code;
std::cout << std::endl;
int array1[16] = { 42011, 42017, 42029, 42045,
42091, 42101, 34001, 34005,
34007, 34009, 34011, 34015,
34033, 10001, 10003, 24015 }; // 0.2387 (23.87%)
int array2[45] = { 11001, 24003, 24510, 24005, 24009,
24013, 24017, 24019, 24021, 24025,
24027, 24029, 24031, 24033, 24035,
24037, 24041, 24043, 51510, 51013,
51043, 51047, 51600, 51059, 51610,
51061, 51069, 51630, 51099, 51107,
51683, 51685, 51153, 51157, 51177,
51179, 51187, 51840, 54003, 54027,
54037, 54065, 42001, 42055, 42133 }; //0.2710 (27.10%)
int * array1_search;
array1_search = std::find(array1, array1+ 16, user_code);
int * array2_search;
array2_search = std::find(array2, array2 + 45, user_code);
if (array1_search != array1+ 16) {
std::cout << "Codefound in Array1: " << *array1_search << '\n';
gas= 0.2387;
}
else if (array2_search != array2_search + 45) {
std::cout << "Code found in Array2: " << *array2_search << '\n';
gas= 0.2710;
}
else {
std::cout << "Not found \n";
gas= 0.1506;
}
Above is my current code. I am trying to have the user input a variable user_code value and then iterate over the two arrays array1[16] and array2[45]. If the user input value is on the first array1 I want to assign gas 0.2387 and if the input value is on the other array2 I want to assign gas 0.2710, and if it is not within any array gas should be 0.1506.
So basically I want to assign a value depending on which array the user's input is contained in. I am very new to c++, what is the best way to go about this?
It seems to work fine if I enter a number that is within array1 or array2 and it correctly identifies that is found in array1 or array2. The problem is when I enter a number I know is not within either array to trigger the else statement it identifies it as being in array2. For example, when I enter 12345 as a user_code it says "Code found in Array2: 0". I know 12345 is not contained in array2 and I do not understand why *array2_search is assigned 0. What can I do to fix this so if a user_code is entered that is not contained within array1 or array2 it goes to the else statement?
else if (array2_search != array2_search + 45) {
Should be
else if (array2_search != array2 + 45) {
or better using std::end of C++11:
if (array1_search != std::end(array1)) {
else if (array2_search != std::end(array2)) {
And int gas; => double gas; if you want to be able to store floating point values, not just integers (0.2387 and 0.2710 would give integer 0).
Using standard containers and newer c++ features if you have C++11 minimum you can then do something like this:
int main() {
// Use Constants Instead of "Hard Coded Values"
// If you noticed these are not even needed.
// const unsigned code1 = 16;
// const unsigned code2 = 45;
// Made gas a float instead of an int due to the decimal values
// I also initialized it with the default value if the code is
// not found in either container.
float gas = 0.1506f; // Default Price If Not Found
// created your first array as a const std::vector<int> and
// used its initializer list to populate its contents: this vector
// can not be modified: remove the const if this container
// will need to have entries added in the future.
const std::vector<int> arr1 { 42011, 42017, 42029, 42045,
42091, 42101, 34001, 34005,
34007, 34009, 34011, 34015,
34033, 10001, 10003, 24015 }; // 0.2387 (23.87%)
// did the same for the second array
const std::vector<int> arr2 { 11001, 24003, 24510, 24005, 24009,
24013, 24017, 24019, 24021, 24025,
24027, 24029, 24031, 24033, 24035,
24037, 24041, 24043, 51510, 51013,
51043, 51047, 51600, 51059, 51610,
51061, 51069, 51630, 51099, 51107,
51683, 51685, 51153, 51157, 51177,
51179, 51187, 51840, 54003, 54027,
54037, 54065, 42001, 42055, 42133 }; //0.2710 (27.10%)
// No changes made here same basic user I/O.
int user_code = 0;
std::cout << "Please enter the Code: ";
std::cin >> user_code;
std::cout << "The value you entered is " << user_code;
std::cout << "\n";
// Created 2 flags for later.
bool b1found = false;
bool b2found = false;
// auto for loop ranged based.
for ( auto code : arr1 ) {
if ( code == user_code ) {
b1found = true; // Set flag
gas = 0.2387f; // Set new gas
// Output code & gas
std::cout << "Code found in Arr1: " << code << '\n';
std::cout << "gas = " << gas << '\n';
}
}
for ( auto code : arr2 ) {
if ( code == user_code ) {
b2found = true; // set flag
gas = 0.2710f; // set gas
// output code & gas
std::cout << "Code found in Arr2: " << code << '\n';
std::cout << "gas = " << gas << '\n';
}
}
// If code not found in either output "not found" and display default gas
if ( !b1found && !b2found ) {
std::cout << "Not found\n";
std::cout << "gas = " << gas << '\n';
}
std::cout << "\nPress any key and enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
You can even simplify this a little more by removing the two bool flags. We know that if a value is found in arr1 or arr2 that the gas value will be changed, So all we really have to do is check to see if it has been changed.
// auto for loop ranged based.
for ( auto code : arr1 ) {
if ( code == user_code ) {
gas = 0.2387f; // Set new gas
// Output code & gas
std::cout << "Code found in Arr1: " << code << '\n';
std::cout << "gas = " << gas << '\n';
}
}
for ( auto code : arr2 ) {
if ( code == user_code ) {
gas = 0.2710f; // set gas
// output code & gas
std::cout << "Code found in Arr2: " << code << '\n';
std::cout << "gas = " << gas << '\n';
}
}
const float defaultGas = 0.1506;
// If code not found in either output "not found" and display default gas
if ( gas == defaultGas ) {
std::cout << "Not found\n";
std::cout << "gas = " << gas << '\n';
}
This is the tag in XML
<content>The Avengers</content>
I want to take out "The Avengers" from the tag using xpath from libxml2 in C++
Here is my code
xmlXPathObject * xpathObj = xmlXPathEvalExpression( (xmlChar*)"/content", xpathCtx );
if ( xpathObj == NULL ) throw "failed to evaluate xpath";
for(int i=0;i<=0; i++)
{
xmlNode *node = NULL;
if ( xpathObj->nodesetval && xpathObj->nodesetval->nodeTab )
{
node = xpathObj->nodesetval->nodeTab[i];
std::cout << "Found the node we want" << std::endl;
}
else
{
throw "failed to find the expected node";
}
xmlAttr *attr = node->properties;
while ( attr )
{
std::cout << "Attribute name: " << attr->name << " value: " << attr->children->content << std::endl;
attr = attr->next;
}
xmlSetProp( node, (xmlChar*)"age", (xmlChar*)"3" );
}
Its work fine and reached easily at content tag but not showing the data between the tag
RESULT
<content age="3">The Avengers</content>
but in console its shows nothing
Found the node we want
Thanks