如何使用WBlock方法将对象导出到新图形的原点

时间:2019-06-24 17:41:27

标签: c# autocad

我需要导出用户选择的一些对象。第一部分已经完成,但是现在我需要将导出的对象放置在新图形的原点上,而无需打开此图形。所选对象的最小范围点必须在原点。

我没有找到如何在外部模型空间上选择对象以及如何将它们移动到原点的方法。

// Create a new external database, where the
// exported objects will be created.
using (var newDb = new Database(true, false))
{    
    // objIds is an array of obejcts selected
    // by the user.
    db.Wblock(newDb, objIds, Point3d.Origin,
                                DuplicateRecordCloning.Ignore);
    newDb.SaveAs(MyFileName, DwgVersion.Newest);
}

// Here it's what I've tried: Open the 
// drawing with the ReadDWgFile method
// and to select all objects from model space,
// but no success.
using (var exDb = new Database(false, false))
{
        try
        {
            exDb.ReadDwgFile(MyFileName,
            FileOpenMode.OpenForReadAndWriteNoShare, false, "");
        }
        catch (System.Exception)
        {
            ed.WriteMessage("\nUnable to read drawing file.");
        }

        using (var exTr = exDb.TransactionManager.StartTransaction())
        {
            // Open the Block table record for read
            BlockTable exBlkTbl;
            exBlkTbl = exTr.GetObject(exDb.BlockTableId,
                                      OpenMode.ForRead) as BlockTable;

            // Open the Block table record Model space for read
            BlockTableRecord exBlkTblRec;
            exBlkTblRec = exTr.GetObject(exBlkTbl[BlockTableRecord.ModelSpace],
               OpenMode.ForRead) as BlockTableRecord;

            // Here I don't know how to proceed to 
            // get the objects from the model space
            // and then move to the origin point.
        }
}

谢谢

3 个答案:

答案 0 :(得分:0)

我建议不要给wblock方法提供一个基点参数等于选择对象的左下角的范围,而不是通过wblock操作重新放置在新图形中创建的对象。您当前正在提供原点作为基点(Point3d.Origin)。

例如,根据答案中发布的代码,您可以尝试提供minExPt变量作为基点参数,例如:

db.Wblock(newDb, objIds, minExPt, DuplicateRecordCloning.Ignore);

这将导致新对象以矩形范围的左下角位于新图形的原点。

答案 1 :(得分:0)

好吧,一旦我找不到修改外部数据库的方法,便以另一种方法找到了解决方案。首先,我将所选对象移动到事务中当前图形的原点。然后,使用wblock方法导出,将原点作为BasePoint参数,最后处理掉没有提交的第一个事务,使对象仍在当前图形的原始点。

顺便说一句,欢迎更好的解决方案。

using (var trMoveToOrigin = db.TransactionManager.StartTransaction())
{
// Get the extents points 
// of the selected objects.
var extPts = trMoveToOrigin.GetExtents(objIdArray);
var minExPt = extPts.MinPoint;

// Get vector from minimal extent point
// to the origin point, that will be
// used to move the selected objects.
Vector3d acVec3d = minExPt.GetVectorTo(Point3d.Origin);

foreach (ObjectId objId in objIds)
{
    Entity e = trMoveToOrigin.GetObject(objId, OpenMode.ForWrite) as Entity;
    e.TransformBy(Matrix3d.Displacement(acVec3d));
}

// Create a new external database, where the
// exported objects will be created.
using (var newDb = new Database(true, false))
{
    using (var trExport = db.TransactionManager.StartTransaction())
    {
        db.Wblock(newDb, objIds, Point3d.Origin,
                            DuplicateRecordCloning.Ignore);
        newDb.SaveAs(FileName, DwgVersion.Newest);
        trExport.Commit();
    }
}

// Dispose without commit, because the
// objects need to be in your original point
// at the end of the program.
trMoveToOrigin.Dispose();
}

答案 2 :(得分:0)

@LeeMac,正如我在回答您的评论时所说,我试图将basepoint参数提供给wblock方法,但没有效果。新图形中的导出对象仍与原始图形中的原始对象位于同一点。

using(Transaction tr = db.TransactionManager.StartTransaction())
{
    // Get the extents points 
    // of the selected objects.
    var extPts = tr.GetExtents(objIdArray);
    var minExPt = extPts.MinPoint;

    // Create a new external database, where the
    // exported objects will be created.
    using (var newDb = new Database(true, false))
    {
        using (var trExport = db.TransactionManager.StartTransaction())
        {
            db.Wblock(newDb, objIds, minExPt, DuplicateRecordCloning.Ignore);
            newDb.SaveAs(FileName, DwgVersion.Newest);
            trExport.Commit();
        }
    }
}

Image: Objects on original drawing selected to be exported

Image: Objects on new drawing still at the same point the original objects

我意识到wblock方法的基点仅在将导出的对象作为块插入图形中时使用。

Image: The minExPt supplied is only used whe the exported objects are inserted as a block

我也测试了WBLOCK命令,并且在单击基点时也发生了同样的情况。