📄 rs_creation.cpp
字号:
minDist = dist; idx = i; } } } if (idx!=-1) { RS_LineData d = poss[idx]->getData(); for (int i=0; i<4; ++i) { if (poss[i]!=NULL) { delete poss[i]; } } if (document!=NULL && handleUndo) { document->startUndoCycle(); } ret = new RS_Line(container, d); ret->setLayerToActive(); ret->setPenToActive(); if (container!=NULL) { container->addEntity(ret); } if (document!=NULL && handleUndo) { document->addUndoable(ret); document->endUndoCycle(); } if (graphicView!=NULL) { graphicView->drawEntity(ret); } } else { ret = NULL; } return ret;}/** * Creates a line with a relative angle to the given entity. * * @param coord Coordinate to define the point where the line should end. * (typically a mouse coordinate). * @param entity Pointer to basis entity. The angle is relative to the * angle of this entity. * @param angle Angle of the line relative to the angle of the basis entity. * @param length Length of the line we're creating. */RS_Line* RS_Creation::createLineRelAngle(const RS_Vector& coord, RS_Entity* entity, double angle, double length) { // check given entity / coord: if (entity==NULL || !coord.valid || (entity->rtti()!=RS2::EntityArc && entity->rtti()!=RS2::EntityCircle && entity->rtti()!=RS2::EntityLine)) { return NULL; } double a1=0.0; switch (entity->rtti()) { case RS2::EntityLine: a1 = ((RS_Line*)entity)->getAngle1(); break; case RS2::EntityArc: a1 = ((RS_Arc*)entity)->getCenter().angleTo(coord) + M_PI/2.0; break; case RS2::EntityCircle: a1 = ((RS_Circle*)entity)->getCenter().angleTo(coord); break; default: // never reached break; } a1 += angle; RS_Vector v1; v1.setPolar(length, a1); //RS_ConstructionLineData(coord-v1, coord+v1); RS_LineData d(coord-v1, coord+v1); RS_Line* ret; if (document!=NULL && handleUndo) { document->startUndoCycle(); } ret = new RS_Line(container, d); ret->setLayerToActive(); ret->setPenToActive(); if (container!=NULL) { container->addEntity(ret); } if (document!=NULL && handleUndo) { document->addUndoable(ret); document->endUndoCycle(); } if (graphicView!=NULL) { graphicView->drawEntity(ret); } return ret;}/** * Creates a polygon with 'number' edges. * * @param center Center of the polygon. * @param corner The first corner of the polygon * @param number Number of edges / corners. */RS_Line* RS_Creation::createPolygon(const RS_Vector& center, const RS_Vector& corner, int number) { // check given coords / number: if (!center.valid || !corner.valid || number<3) { return NULL; } RS_Line* ret = NULL; if (document!=NULL && handleUndo) { document->startUndoCycle(); } RS_Vector c1(false); RS_Vector c2 = corner; RS_Line* line; for (int n=1; n<=number; ++n) { c1 = c2; c2 = c2.rotate(center, (M_PI*2)/number); line = new RS_Line(container, RS_LineData(c1, c2)); line->setLayerToActive(); line->setPenToActive(); if (ret==NULL) { ret = line; } if (container!=NULL) { container->addEntity(line); } if (document!=NULL && handleUndo) { document->addUndoable(line); } if (graphicView!=NULL) { graphicView->drawEntity(line); } } if (document!=NULL && handleUndo) { document->endUndoCycle(); } return ret;}/** * Creates a polygon with 'number' edges. * * @param corner1 The first corner of the polygon. * @param corner2 The second corner of the polygon. * @param number Number of edges / corners. */RS_Line* RS_Creation::createPolygon2(const RS_Vector& corner1, const RS_Vector& corner2, int number) { // check given coords / number: if (!corner1.valid || !corner2.valid || number<3) { return NULL; } RS_Line* ret = NULL; if (document!=NULL && handleUndo) { document->startUndoCycle(); } double len = corner1.distanceTo(corner2); double ang1 = corner1.angleTo(corner2); double ang = ang1; RS_Vector c1(false); RS_Vector c2 = corner1; RS_Vector edge; RS_Line* line; for (int n=1; n<=number; ++n) { c1 = c2; edge.setPolar(len, ang); c2 = c1 + edge; line = new RS_Line(container, RS_LineData(c1, c2)); line->setLayerToActive(); line->setPenToActive(); if (ret==NULL) { ret = line; } if (container!=NULL) { container->addEntity(line); } if (document!=NULL && handleUndo) { document->addUndoable(line); } if (graphicView!=NULL) { graphicView->drawEntity(line); } // more accurate than incrementing the angle: ang = ang1 + (2*M_PI)/number*n; } if (document!=NULL && handleUndo) { document->endUndoCycle(); } return ret;}/** * Creates an insert with the given data. * * @param data Insert data (position, block name, ..) */RS_Insert* RS_Creation::createInsert(RS_InsertData& data) { RS_DEBUG->print("RS_Creation::createInsert"); if (document!=NULL && handleUndo) { document->startUndoCycle(); } RS_Insert* ins = new RS_Insert(container, data); // inserts are also on layers ins->setLayerToActive(); ins->setPenToActive(); if (container!=NULL) { container->addEntity(ins); } if (document!=NULL && handleUndo) { document->addUndoable(ins); document->endUndoCycle(); } if (graphicView!=NULL) { graphicView->drawEntity(ins); } RS_DEBUG->print("RS_Creation::createInsert: OK"); return ins;}/** * Creates an image with the given data. */RS_Image* RS_Creation::createImage(RS_ImageData& data) { if (document!=NULL && handleUndo) { document->startUndoCycle(); } RS_Image* img = new RS_Image(container, data); img->setLayerToActive(); img->setPenToActive(); img->update(); if (container!=NULL) { container->addEntity(img); } if (document!=NULL && handleUndo) { document->addUndoable(img); document->endUndoCycle(); } if (graphicView!=NULL) { graphicView->drawEntity(img); } return img;}/** * Creates a new block from the currently selected entitiies. * * @param referencePoint Reference point for the block. * @param name Block name * @param remove true: remove existing entities, false: don't touch entities */RS_Block* RS_Creation::createBlock(const RS_BlockData& data, const RS_Vector& referencePoint, const bool remove) { // start undo cycle for the container if we're deleting the existing entities if (remove && document!=NULL) { document->startUndoCycle(); } RS_Block* block = new RS_Block(container, RS_BlockData(data.name, data.basePoint, data.frozen)); // copy entities into a block for (RS_Entity* e=container->firstEntity(); e!=NULL; e=container->nextEntity()) { //for (uint i=0; i<container->count(); ++i) { //RS_Entity* e = container->entityAt(i); if (e!=NULL && e->isSelected()) { // delete / redraw entity in graphic view: if (remove) { if (graphicView!=NULL) { graphicView->deleteEntity(e); } e->setSelected(false); } else { if (graphicView!=NULL) { graphicView->deleteEntity(e); } e->setSelected(false); if (graphicView!=NULL) { graphicView->drawEntity(e); } } // add entity to block: RS_Entity* c = e->clone(); c->move(-referencePoint); block->addEntity(c); if (remove) { //container->removeEntity(e); //i=0; e->changeUndoState(); if (document!=NULL) { document->addUndoable(e); } } } } if (remove && document!=NULL) { document->endUndoCycle(); } if (graphic!=NULL) { graphic->addBlock(block); } return block;}/** * Inserts a library item from the given path into the drawing. */RS_Insert* RS_Creation::createLibraryInsert(RS_LibraryInsertData& data) { RS_DEBUG->print("RS_Creation::createLibraryInsert"); RS_Graphic g; if (!g.open(data.file, RS2::FormatUnknown)) { RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Creation::createLibraryInsert: Cannot open file: %s"); return NULL; } // unit conversion: if (graphic!=NULL) { double uf = RS_Units::convert(1.0, g.getUnit(), graphic->getUnit()); g.scale(RS_Vector(0.0, 0.0), RS_Vector(uf, uf)); } //g.scale(RS_Vector(data.factor, data.factor)); //g.rotate(data.angle); RS_String s;#if QT_VERSION>=0x030000 s = RS_FileInfo(data.file).baseName(true);#else s = data.file.left(data.file.length()-4);#endif RS_Modification m(*container, graphicView); m.paste( RS_PasteData( data.insertionPoint, data.factor, data.angle, true, s), &g); RS_DEBUG->print("RS_Creation::createLibraryInsert: OK"); return NULL;}// EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -