QCAD 代码片段(一)

Draw 绘制


    var di = this.getDocumentInterface();
    var document = this.getDocument();
    var op = new RAddObjectsOperation();
    for (var h=0; h<=255; h+=16) {
        for (var v=0; v<=255; v+=16) {
            for (var s=0; s<=255; s+=16) {
                // RShape-->RLine
                var line = new RLine(new RVector(h/16, v/16+s), new RVector(h/16, v/16+s+0.5));
                // RLineData-->REntiy
                var lineEntity = new RLineEntity(document, new RLineData(line));
                // 添加扩展属性
                lineEntiy.setProperty(RPropertyTypeId("扩展属性","长度"),10000);
                var proAttr=new RPropertyAttributes();
                proAttr.setOption(RPropertyAttributes.Integer,true);
                lineEntiy.setCustomPropertyAttributes("扩展属性","长度",proAttr);
                var c = new RColor();
                c.setHsv(h,s,v);
                lineEntity.setColor(c);
                //
                op.addObject(lineEntity, false);
            }
        }
    }
    di.applyOperation(op);

RMainWindowQt & RMainWindow & RDocument

// Creating the storage underneath the document:
var storage = new RMemoryStorage();
 
// Creating the spatial index:
var spatialIndex = new RSpatialIndexNavel();
 
// Creating the document:
var document = new RDocument(storage, spatialIndex);
 
// Creating the document interface 
// (required for import / export, graphics scenes and views):
var documentInterface = new RDocumentInterface(document);
// create drawing document with document interface:
    RMemoryStorage* storage = new RMemoryStorage();
    RSpatialIndexNavel* spatialIndex = new RSpatialIndexNavel();
    RDocument* document = new RDocument(*storage, *spatialIndex);
    documentInterface = new RDocumentInterface(*document);

RLayer 创建图层

// add a layer 'MyLayer':
    QSharedPointer<RLayer> layer = 
        QSharedPointer<RLayer>(
            new RLayer(document, "MyLayer")
        );
    layer->setLinetypeId(document->getLinetypeId("continuous"));
    layer->setLineweight(RLineweight::Weight025);
    layer->setColor(RColor(255,0,0));

Draw 绘制线


 // create and add a line entity:
    QSharedPointer<RLineEntity> lineEntity = 
        QSharedPointer<RLineEntity>(
            new RLineEntity(document, RLineData(RVector(0,0), RVector(100,0)))
        );

    // set attributes:
    lineEntity->setLayerId(layerId);
    lineEntity->setColor(RColor(0,128,255));
    lineEntity->setLinetypeId(document->getLinetypeId("dashed"));
    lineEntity->setLineweight(RLineweight::Weight100);

    // set a custom property:
    lineEntity->setCustomProperty("MyApp", "MyIntProperty", 77);
    lineEntity->setCustomProperty("MyApp", "MyStringProperty", "Some text");

    op = new RAddObjectOperation(
        lineEntity, 
        false       // false: don't use current attributes but 
                    //        custom attributes set above
    );

    // add the line entity to the drawing:
    documentInterface->applyOperation(op);

Draw 绘制点

    RAddObjectsOperation*op=new RAddObjectsOperation();
    QSharedPointer<RPointEntity> pointEntity =
            QSharedPointer<RPointEntity>(new RPointEntity(this->getDocument(), RPointData(_point)));
    op->addObject(pointEntity);
    this->documentInterface->applyOperation(op);

Scene&View

var graphicsScene = new RGraphicsSceneQt(documentInterface)
var graphicsView = new RGraphicsViewQt();
graphicsView.setScene(graphicsScene);
var di = this.getDocumentInterface();
var view = di.getLastKnownViewWithFocus();
view = view.getRGraphicsView();

User Input 用户输入

    RDocumentInterface* di = RMainWindow::getDocumentInterfaceStatic();
    di->setCurrentAction(new RPointAction(guiAction));

多线程绘制

int painterThreadCount=6;
QList<RIdBox > entities;
int slice = int(floor(double(entities.length())/painterThreadCount));
QList<QFuture<void> > futureThread;
for (int threadId=0; threadId<painterThreadCount; threadId++) {
    int start = threadId*slice;
    int end = (threadId+1)*slice;
    if (threadId==painterThreadCount-1) {
        end = list.length();
    }
    //qDebug() << "slice:" << start << end;
    futureThread.append(QtConcurrent::run(this, &RGraphicsViewImage::paintEntitiesThread, list, start, end));
}
//RDebug::stopTimer(100, "launch threads");

//RDebug::startTimer(100);
for (int i=0; i<futureThread.length(); i++) {
    futureThread[i].waitForFinished();
}

//
void RGraphicsViewImage::paintEntitiesThread( QList<REntity::Id>& list, int start, int end) {
    for (int i=start; i<end; i++) {
        paintEntityThread(list[i]);
    }
}

void RGraphicsViewImage::paintEntityThread(REntity::Id id){
    QSharedPointer<REntity> entity = storage.queryEntityDirect(id);
    if (entity.isNull()) {
        return;
    }
    if (entity->isUndone()) {
        return;
    }

    entity->update();

    RObject::Id entityId = entity->getId();
    QList<RBox> bbs = entity->getBoundingBoxes(true);
    


}
 QSet<REntity::Id> ids = document->queryAllBlockReferences();
    QList<REntity::Id> list=document->getStorage().orderBackToFront(ids);
    //
    //RSettings.getIntValue("GraphicsView/Threads", Math.max(RS.getIdealThreadCount(), 6));
    int threadCount=RSettings::getIntValue("GraphicsView/Threads",qMax(QThread::idealThreadCount(), 10));
    int slice=int(floor(double((list.length()))/threadCount));
    QList<QFuture<void> >futureThread;
    for(int threadId=0;threadId<threadCount;threadId++){
        int start = threadId*slice;
        int end = (threadId+1)*slice;
        if (threadId==threadCount-1) {
            end = list.length();
        }
        qDebug() << "slice:" << start << end;
        futureThread.append(QtConcurrent::run(this,&RDxfImporter::importEntitiesThread, list, start, end));
    }
    for (int i=0; i<futureThread.length(); i++) {
        futureThread[i].waitForFinished();
    }
void RDxfImporter::importEntitiesThread(QList<RObject::Id> &list, int start, int end)
{

    for (int i=start; i<end; i++) {
        RBlockReferenceEntity::Id id = list[i];
        QSharedPointer<REntity> entity = document->queryEntityDirect(id);
        if (entity.isNull()) {
            continue;
        }
        QSharedPointer<RBlockReferenceEntity> blockRef = entity.dynamicCast<RBlockReferenceEntity>();
        if (blockRef.isNull()) {
            continue;
        }

        QList<RBox> bbs = blockRef->getBoundingBoxes();
        QVariant v = blockRef->getCustomProperty("", "block");
        if (!v.isValid()) {
            continue;
        }
        QString blockName = v.toString();
        RBlock::Id blockId = document->getBlockId(blockName);

        if (blockId==RBlock::INVALID_ID) {
            continue;
        }

        blockRef->setReferencedBlockId(blockId);

        document->removeFromSpatialIndex(blockRef, bbs);
        document->addToSpatialIndex(blockRef);
    }

}

获取指定窗口的控件的值

var appWin=RMainWindowQt.getMainWindow();
var dock=appWin.findChild("FilterEditorDock");
var entityTypeCombo=dock.findChild("comboBoxEntityType");
var value=entityTypeCombo.currentIndex;

查询选择实体的属性值

var di=EAction.getDocumentInterface();
var doc = di.getDocument();
var entities = doc.querySelectedEntities();
var storage=doc.getStorage();
var entity=storage.queryEntityDirect(entities[0]);
entity.getProperty(RBlockReferenceEntity.PropertyBlock)
entity.getProperty(RTextEntity.PropertyText)
entity.getProperty(REntity.PropertyType)[1].isStyle()

警告对话框

{
var appWin = EAction.getMainWindow();
QMessageBox.warning(appWin,qsTr("Warning"),"warning");
}

确认对话框

var appWin = EAction.getMainWindow();
var buttons = new QMessageBox.StandardButtons(QMessageBox.Yes, QMessageBox.No);
var ret = QMessageBox.warning(app, 
    qsTr("Delete layer?"), 
    qsTr("Do you wish to delete  \\"%1\\" layer?").arg(fileToSave),
    buttons);
if (ret!=QMessageBox.Yes) {
    
}

获取图层

var di = EAction.getDocumentInterface();
var doc=di.getDocument();
var layer=doc.queryLayer("0");
layer.isProtected();

导入导出文件和修改

var storage = new RMemoryStorage();
var spatialIndex = new RSpatialIndexSimple();
var doc = new RDocument(storage, spatialIndex);
var di = new RDocumentInterface(doc);
di.importFile("in.dxf");

var op = new RModifyObjectsOperation();

var ids = doc.queryAllEntities();
for (var i=0; i<ids.length; i++) {
    var id = ids[i];
    var entity = doc.queryEntity(id);
    entity.setColor(new RColor("blue"));
    op.addObject(entity, false);
}
di.applyOperation(op);
di.exportFile("out.dxf", "R24 (2010) DXF");

监听导出事件

var adapter = new RExportListenerAdapter();

// register export listener:
var appWin = EAction.getMainWindow();
appWin.addExportListener(adapter);

// connect postExport signal to function ExExportListener.fileExported:
adapter.postExport.connect(function(documentInterface){
    if(isNull(documentInterface)){
        return;
    }
    print("file saved: ",documentInterface.getDocument().getFileName());
});

事务监听

// create a new transaction listener:
    var adapter = new RTransactionListenerAdapter();
    var appWin = EAction.getMainWindow();
    appWin.addTransactionListener(adapter);

    // connect transactionUpdated signal to a function that prints
    // information about the transaction:
    adapter.transactionUpdated.connect(function(document, transaction) {
        if (isNull(document) || isNull(transaction)) {
            return;
        }

        var objIds = transaction.getAffectedObjects();
        EAction.handleUserMessage(qsTr("Transaction affected %n object(s).", "", objIds.length));
        if (objIds.length<10) {
            EAction.handleUserMessage("Object IDs: " + objIds.join(','));
        }

        var i, objId;

        // show property changes of first 10 entities:
        for (i=0; i<objIds.length && i<10; i++) {
            objId = objIds[i];

            EAction.handleUserMessage("Changed object ID: " + objId);

            var pre = "&nbsp;&nbsp;&nbsp;";

            var changes = transaction.getPropertyChanges(objId);
            for (var k=0; k<changes.length; k++) {
                var change = changes[k];
                var pid = change.getPropertyTypeId();

                if (pid.isCustom()) {
                    // custom property was changed:
                    EAction.handleUserMessage(pre + "custom property:" + pid.getPropertyTitle(), false);
                }
                else {
                    // other property:
                    // see RObject::Property*, REntity::Property*, RLineEntity::Property*, etc:
                    EAction.handleUserMessage(pre + "property type:" + pid, false);
                }

                EAction.handleUserMessage(pre + "old value:" + change.getOldValue(), false);
                EAction.handleUserMessage(pre + "new value:" + change.getNewValue(), false);
            }
        }

        // show objects that were created or deleted:
        objIds = transaction.getStatusChanges();
        for (i=0; i<objIds.length && i<10; i++) {
            objId = objIds[i];

            var obj = document.queryObjectDirect(objId);
            //isUndone==delete
            if (obj.isUndone()) {
                EAction.handleUserMessage("Deleted object with ID: " + objId);
            }
            else {
                EAction.handleUserMessage("Created object with ID: " + objId);
            }

            if (isLayer(obj)) {
                EAction.handleUserMessage("Object is layer");
            }
            else if (isBlock(obj)) {
                EAction.handleUserMessage("Object is block");
            }
            else if (isEntity(obj)) {
                EAction.handleUserMessage("Object is entity");
            }
        }
    });

增加删除 Entity Object

var di = this.getDocumentInterface();
var document = this.getDocument();

var op, entity, id;

// add line entity,
// document takes ownership of entity:
op = new RAddObjectsOperation();
entity = new RLineEntity(document, new RLineData(new RVector(0,0), new RVector(100,100)));
op.addObject(entity);
di.applyOperation(op);

// after applying the operation, the ID of the newly added entity is known
// and can be stored as reference for the added entity:
id = entity.getId();
EAction.handleUserMessage(qsTr("Added line with ID %1".arg(id)));

// query a copy of the stored entity from the document
// and use it to delete the entity:
entity = document.queryEntity(id);
op = new RDeleteObjectsOperation();
op.deleteObject(entity);
di.applyOperation(op);

EAction.handleUserMessage(qsTr("Deleted line with ID %1").arg(id));
EAction.handleUserMessage(qsTr("You can use <i>Edit > Undo</i> to restore the line entity."), false);

添加自定义属性

 //dataType
    var type_property=new RPropertyTypeId("app","group");
    entity.setProperty(type_property,group);
    var type_attri=new RPropertyAttributes();
    type_attri.setOption(RPropertyAttributes.Custom,true);
    type_attri.setReadOnly(true);
    type_attri.setInvisible(true);
    entity.setCustomPropertyAttributes("app","group",type_attri);

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片