ARX,C#选择CAD对象并创建DWG文件

/// 
/// 创建的文件路径、所有的实体对象、图形原点
/// 
/// 
/// 
/// 
/// 
private bool CreateDwgFileBySelEntis(string fileName,ObjectIdCollection enIds,Point3d insertPoint)
{
    Document currentDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;//获取当前的活动文档
    Autodesk.AutoCAD.DatabaseServices.Database newDb = new Autodesk.AutoCAD.DatabaseServices.Database();//dwgDoc.Database;

    using (Transaction destTrans = newDb.TransactionManager.StartTransaction())
    {
        var blockTable = destTrans.GetObject(newDb.BlockTableId, OpenMode.ForRead) as BlockTable;
        var blockTableRecord = destTrans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
        IdMapping idmap = new IdMapping();

        using (Transaction trans = currentDoc.TransactionManager.StartTransaction())
        {
            try
            {
                //来源数据库.wblockcloneobjec(目标数据库);
                currentDoc.Database.WblockCloneObjects(enIds, blockTableRecord.ObjectId, idmap, DuplicateRecordCloning.Ignore, false);
                // 获取复制的所有对象 转化为 ObjectIdCollection  用于正交变换
                List cloneIds = new List();
                foreach (ObjectId id in blockTableRecord)
                {
                    cloneIds.Add(id);
                }
                ObjectIdCollection cloneCollection = new ObjectIdCollection(cloneIds.ToArray());
                // 坐标变换
                Matrix3d matrix3d = Matrix3d.AlignCoordinateSystem(insertPoint, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, new Point3d(0, 0, 0), Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis);
                EntityUtil.TransformBy(cloneCollection, matrix3d);
                newDb.SaveAs(fileName, DwgVersion.AC1800);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                var editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                editor.WriteMessage("克隆错误:" + exception.Message);

                destTrans.Commit();
                trans.Commit();
                return false;
            }
            trans.Commit();
        }
        destTrans.Commit();
    }
    return true;
}

你可能感兴趣的:(CAD,ARX,c#,windows)