Exemple #1
0
        /// <summary>
        /// 删除实体的缩放比例
        /// </summary>
        /// <param name="entId">实体的Id</param>
        /// <param name="scaleNames">缩放比例名列表</param>
        public static void RemoveScale(this ObjectId entId, params string[] scaleNames)
        {
            //获取entId指示的实体对象
            DBObject obj = entId.GetObject(OpenMode.ForRead);

            //如果实体对象支持注释缩放
            if (obj.Annotative != AnnotativeStates.NotApplicable)
            {
                //获得对象所有的注释比例
                List <ObjectContext> scales = entId.GetAllScales();
                obj.UpgradeOpen();//切换实体为写的状态
                //获取当前图形的对象比例管理器
                ObjectContextManager ocm = obj.Database.ObjectContextManager;
                //获取当前图形的注释比例列表,名为ACDB_ANNOTATIONSCALES
                ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
                //遍历需要设置的注释比例
                foreach (string scaleName in scaleNames)
                {
                    //获取名为scaleName的注释比例
                    ObjectContext scale = occ.GetContext(scaleName);
                    //若不存在scaleName的注释比例,则结束本次循环
                    if (scale == null)
                    {
                        continue;
                    }
                    //删除名为scaleName的注释比例
                    ObjectContexts.RemoveContext(obj, scale);
                }
                obj.DowngradeOpen();//为了安全将实体切换为读的状态
            }
        }
Exemple #2
0
        /// <summary>
        /// 在AutoCAD图形中插入块参照
        /// </summary>
        /// <param name="spaceId">块参照要加入的模型空间或图纸空间的Id</param>
        /// <param name="layer">块参照要加入的图层名</param>
        /// <param name="blockName">块参照所属的块名</param>
        /// <param name="position">插入点</param>
        /// <param name="scale">缩放比例</param>
        /// <param name="rotateAngle">旋转角度</param>
        /// <returns>返回块参照的Id</returns>
        public static ObjectId InsertBlockReference(this ObjectId spaceId, string layer, string blockName, Point3d position, Scale3d scale, double rotateAngle)
        {
            ObjectId blockRefId;            //存储要插入的块参照的Id
            Database db = spaceId.Database; //获取数据库对象
            //以读的方式打开块表
            BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);

            //如果没有blockName表示的块,则程序返回
            if (!bt.Has(blockName))
            {
                return(ObjectId.Null);
            }
            //以写的方式打开空间(模型空间或图纸空间)
            BlockTableRecord space = (BlockTableRecord)spaceId.GetObject(OpenMode.ForWrite);
            //创建一个块参照并设置插入点
            BlockReference br = new BlockReference(position, bt[blockName]);

            br.ScaleFactors = scale;        //设置块参照的缩放比例
            br.Layer        = layer;        //设置块参照的层名
            br.Rotation     = rotateAngle;  //设置块参照的旋转角度
            ObjectId btrId = bt[blockName]; //获取块表记录的Id
            //打开块表记录
            BlockTableRecord record = (BlockTableRecord)btrId.GetObject(OpenMode.ForRead);

            //添加可缩放性支持
            if (record.Annotative == AnnotativeStates.True)
            {
                ObjectContextCollection contextCollection = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");
                ObjectContexts.AddContext(br, contextCollection.GetContext("1:1"));
            }
            blockRefId = space.AppendEntity(br);                     //在空间中加入创建的块参照
            db.TransactionManager.AddNewlyCreatedDBObject(br, true); //通知事务处理加入创建的块参照
            space.DowngradeOpen();                                   //为了安全,将块表状态改为读
            return(blockRefId);                                      //返回添加的块参照的Id
        }
        /// <summary>
        /// 在AutoCAD图形中插入块参照
        /// </summary>
        /// <param name="spaceId">块参照要加入的模型空间或图纸空间的Id</param>
        /// <param name="layer">块参照要加入的图层名</param>
        /// <param name="blockName">块参照所属的块名</param>
        /// <param name="position">插入点</param>
        /// <param name="scale">缩放比例</param>
        /// <param name="rotateAngle">旋转角度</param>
        /// <param name="attNameValues">属性的名称与取值</param>
        /// <returns>返回块参照的Id</returns>
        public static ObjectId InsertBlockReference(this ObjectId spaceId, string layer, string blockName, Point3d position, Scale3d scale, double rotateAngle)
        {
            DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
            ObjectId     blockRefId;
            Database     db = spaceId.Database;
            BlockTable   bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);

            if (!bt.Has(blockName))
            {
                return(ObjectId.Null);
            }
            BlockTableRecord space = (BlockTableRecord)spaceId.GetObject(OpenMode.ForWrite);
            BlockReference   br    = new BlockReference(position, bt[blockName]);

            br.ScaleFactors = scale;
            br.Layer        = layer;
            br.Rotation     = rotateAngle;
            ObjectId         btrId  = bt[blockName];
            BlockTableRecord record = (BlockTableRecord)btrId.GetObject(OpenMode.ForRead);

            if (record.Annotative == AnnotativeStates.True)
            {
                ObjectContextCollection contextCollection = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");
                ObjectContexts.AddContext(br, contextCollection.GetContext("1:1"));
            }
            blockRefId = space.AppendEntity(br);
            db.TransactionManager.AddNewlyCreatedDBObject(br, true);
            space.DowngradeOpen();
            docLock.Dispose();
            return(blockRefId);
        }
Exemple #4
0
        public static ObjectId InsertBlock(Point3d anchorPoint, double Rotation, ObjectId BlockID)
        {
            // Get the current document and database
            Document acDoc              = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb            = acDoc.Database;
            ObjectContextCollection occ = acCurDb.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");

            Transaction acTrans = acDoc.TransactionManager.TopTransaction;

            // Open the Block table for read
            BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

            BlockTableRecord blockDef = BlockID.GetObject(OpenMode.ForRead) as BlockTableRecord;

            // Open the Block table record Model space for write
            BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            // Insert the block into the current space
            using (BlockReference acBlkRef = new BlockReference(anchorPoint, BlockID))
            {
                Matrix3d           curUCSMatrix = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
                CoordinateSystem3d curUCS       = curUCSMatrix.CoordinateSystem3d;

                acBlkRef.TransformBy(Matrix3d.Rotation(Rotation, curUCS.Zaxis, anchorPoint));
                //For some unknown reason this is needed in Civil3D
                //TODO: Find reason below line is needed and fix it
                acBlkRef.TransformBy(Matrix3d.Scaling(1000, anchorPoint));

                acBlkRef.AddContext(occ.GetContext("1:1"));

                acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                acBlkTblRec.AppendEntity(acBlkRef);
                acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

                // AttributeDefinitions
                foreach (ObjectId id in blockDef)
                {
                    DBObject            obj    = id.GetObject(OpenMode.ForRead);
                    AttributeDefinition attDef = obj as AttributeDefinition;
                    if ((attDef != null) && (!attDef.Constant))
                    {
                        //This is a non-constant AttributeDefinition
                        //Create a new AttributeReference
                        using (AttributeReference attRef = new AttributeReference())
                        {
                            attRef.SetAttributeFromBlock(attDef, acBlkRef.BlockTransform);
                            attRef.TextString = "0";
                            //Add the AttributeReference to the BlockReference
                            acBlkRef.AttributeCollection.AppendAttribute(attRef);
                            acTrans.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }
                }

                return(acBlkRef.ObjectId);
            }
        }
Exemple #5
0
        /// <summary>
        /// 为实体添加指定的注释比例
        /// </summary>
        /// <param name="entId">实体的Id</param>
        /// <param name="scaleNames">注释比例名列表</param>
        public static void AttachScale(this ObjectId entId, params string[] scaleNames)
        {
            Database db = entId.Database;
            //获取entId指示的实体对象
            DBObject obj = entId.GetObject(OpenMode.ForRead);

            //如果实体对象支持注释缩放
            if (obj.Annotative != AnnotativeStates.NotApplicable)
            {
                //如果实体为块参照,则需要通过其所属的块表记录来设置可注释缩放
                if (obj is BlockReference)
                {
                    BlockReference br = obj as BlockReference;
                    //打开对应的块表记录
                    BlockTableRecord btr = (BlockTableRecord)br.BlockTableRecord.GetObject(OpenMode.ForWrite);
                    //设置块表记录为可注释缩放
                    btr.Annotative = AnnotativeStates.True;
                }
                //其它可注释缩放的实体,使其可注释缩放
                else if (obj.Annotative == AnnotativeStates.False)
                {
                    obj.Annotative = AnnotativeStates.True;
                }
                obj.UpgradeOpen();//切换实体为写的状态以添加注释比例
                //获取当前图形的对象比例管理器
                ObjectContextManager ocm = db.ObjectContextManager;
                //获取当前图形的注释比例列表,名为ACDB_ANNOTATIONSCALES
                ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
                //遍历需要设置的注释比例
                foreach (string scaleName in scaleNames)
                {
                    //获取名为scaleName的注释比例
                    ObjectContext scale = occ.GetContext(scaleName);
                    //若不存在scaleName的注释比例,则结束本次循环
                    if (scale == null)
                    {
                        continue;
                    }
                    //为实体添加名为scaleName的注释比例
                    ObjectContexts.AddContext(obj, scale);
                }
                obj.DowngradeOpen();//为了安全将实体切换为读的状态
            }
        }
Exemple #6
0
        /// <summary>
        /// Type "CANNOSCALE" to get current scale
        /// </summary>
        /// <param name="scalename"></param>
        /// <returns></returns>
        public static AnnotationScale GetAnnoScale(string scalename)
        {
            if (scalename == "CANNOSCALE")
            {
                scalename = App.GetSystemVariable("CANNOSCALE").ToString();
            }

            Document acDoc   = App.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            AnnotationScale asc = new AnnotationScale();

            ObjectContextManager ocm = acCurDb.ObjectContextManager;

            if (ocm != null)
            {
                ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
                if (occ != null)
                {
                    asc = occ.GetContext(scalename) as AnnotationScale;
                }
            }
            return(asc);
        }
Exemple #7
0
        public static void TagFoundations(List <Curve> lines, Plot p)
        {
            Database acCurDb;

            acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
            ObjectContextCollection occ = acCurDb.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId   current  = acCurDb.Clayer;
                acCurDb.Clayer = acLyrTbl[Main.FoundationTextLayer];

                // Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                ObjectId blkRecId = ObjectId.Null;

                if (!acBlkTbl.Has("FormationTag"))
                {
                    Main.LoadBlocks();
                }

                foreach (Curve c in lines)
                {
                    Matrix3d           curUCSMatrix = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
                    CoordinateSystem3d curUCS       = curUCSMatrix.CoordinateSystem3d;

                    //Get lable point
                    Point3d labelPoint3d = c.GetPointAtDist(c.GetDistanceAtParameter(c.EndParam) / 2);
                    Point3d labelPoint   = new Point3d(labelPoint3d.X, labelPoint3d.Y, 0);

                    // Insert the block into the current space
                    using (BlockReference acBlkRef = new BlockReference(labelPoint, acBlkTbl["FormationTag"]))
                    {
                        //Calculate Line Angle
                        double y     = c.EndPoint.Y - c.StartPoint.Y;
                        double x     = c.EndPoint.X - c.StartPoint.X;
                        double angle = Math.Atan(Math.Abs(y) / Math.Abs(x));
                        if (angle >= Math.PI / 2)
                        {
                            acBlkRef.TransformBy(Matrix3d.Rotation(0, curUCS.Zaxis, labelPoint));
                        }
                        else
                        {
                            acBlkRef.TransformBy(Matrix3d.Rotation(Math.PI / 2, curUCS.Zaxis, labelPoint));
                        }
                        acBlkRef.AddContext(occ.GetContext("10:1"));

                        //Set value
                        AttributeCollection attCol = acBlkRef.AttributeCollection;
                        foreach (ObjectId attId in attCol)
                        {
                            AttributeReference att = acTrans.GetObject(attId, OpenMode.ForRead, false) as AttributeReference;
                            if (att.Tag == "LEVEL")
                            {
                                att.UpgradeOpen();
                                att.TextString = p.FormationLevel.ToString();
                            }
                        }

                        BlockTableRecord acCurSpaceBlkTblRec;
                        acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
                        acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
                    }
                }

                acCurDb.Clayer = current;

                acTrans.Commit();
            }
        }
Exemple #8
0
        public void Generate()
        {
            Database acCurDb;

            acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
            ObjectContextCollection occ = acCurDb.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");

            Transaction acTrans = acCurDb.TransactionManager.TopTransaction;

            //Centre line
            Curve c = acTrans.GetObject(ObjectId, OpenMode.ForRead) as Curve;

            //Offset it
            LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
            ObjectId   current  = acCurDb.Clayer;

            acCurDb.Clayer = acLyrTbl[Utilities.FoundationLayer];

            // Open the Block table for read
            BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

            // Open the Block table record Model space for write
            BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            DBObjectCollection offsets  = c.GetOffsetCurves(FoundationWidth / 2);
            DBObjectCollection offsets2 = c.GetOffsetCurves(-(FoundationWidth / 2));

            foreach (Entity e in offsets)
            {
                acBlkTblRec.AppendEntity(e);
                acTrans.AddNewlyCreatedDBObject(e, true);
                e.LayerId            = acCurDb.Clayer;
                PositiveFoundationId = e.ObjectId;
            }
            foreach (Entity e in offsets2)
            {
                acBlkTblRec.AppendEntity(e);
                acTrans.AddNewlyCreatedDBObject(e, true);
                e.LayerId            = acCurDb.Clayer;
                NegativeFoundationId = e.ObjectId;
            }


            acCurDb.Clayer = current;

            //Tag it
            acCurDb.Clayer = acLyrTbl[Utilities.FoundationTextLayer];

            //Add foundation tag
            Matrix3d           curUCSMatrix = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
            CoordinateSystem3d curUCS       = curUCSMatrix.CoordinateSystem3d;

            //Get lable point
            Point3d labelPoint3d = c.GetPointAtDist(c.GetDistanceAtParameter(c.EndParam) / 2);
            Point3d labelPoint   = new Point3d(labelPoint3d.X, labelPoint3d.Y, 0);

            BlockTableRecord blockDef = acBlkTbl["FormationTag"].GetObject(OpenMode.ForRead) as BlockTableRecord;

            // Insert the block into the current space
            using (BlockReference acBlkRef = new BlockReference(labelPoint, acBlkTbl["FormationTag"]))
            {
                //Calculate Line Angle
                double y     = c.EndPoint.Y - c.StartPoint.Y;
                double x     = c.EndPoint.X - c.StartPoint.X;
                double angle = Math.Atan(Math.Abs(y) / Math.Abs(x));
                if (angle >= Math.PI / 4)
                {
                    acBlkRef.TransformBy(Matrix3d.Rotation(0, curUCS.Zaxis, labelPoint));
                }
                else
                {
                    acBlkRef.TransformBy(Matrix3d.Rotation(Math.PI / 2, curUCS.Zaxis, labelPoint));
                }
                acBlkRef.AddContext(occ.GetContext("10:1"));

                acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                acBlkTblRec.AppendEntity(acBlkRef);
                acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

                // AttributeDefinitions
                foreach (ObjectId id in blockDef)
                {
                    DBObject            obj    = id.GetObject(OpenMode.ForRead);
                    AttributeDefinition attDef = obj as AttributeDefinition;
                    if ((attDef != null) && (!attDef.Constant))
                    {
                        //This is a non-constant AttributeDefinition
                        //Create a new AttributeReference
                        using (AttributeReference attRef = new AttributeReference())
                        {
                            attRef.SetAttributeFromBlock(attDef, acBlkRef.BlockTransform);
                            attRef.TextString = Parent.FormationLevel.ToString("F3");
                            //Add the AttributeReference to the BlockReference
                            acBlkRef.AttributeCollection.AppendAttribute(attRef);
                            acTrans.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }
                }

                FormationTagId = acBlkRef.ObjectId;
            }

            acCurDb.Clayer = current;
        }