Пример #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();//为了安全将实体切换为读的状态
            }
        }
Пример #2
0
        public CartogrammLabels(SimpleGride gride)
        {
            _gride = gride;

            TopRow    = new List <TableField>();
            BottomRow = new List <TableField>();

            PreTopRow       = new List <TableField>();
            AmountTopRow    = new List <TableField>();
            PreBottomRow    = new List <TableField>();
            AmountBottomRow = new List <TableField>();

            ObjectContextManager    ocm = HostApplicationServices.WorkingDatabase.ObjectContextManager;
            ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

            _scale = (AnnotationScale)occ.CurrentContext;

            _tableRowHeight       /= _scale.Scale;
            _firstColumnWidth     /= _scale.Scale;
            _preOrPostColumnWidth /= _scale.Scale;

            _tableTextHeight  /= _scale.Scale;
            _grideTextHeight  /= _scale.Scale;
            _volumeTextHeight /= _scale.Scale;

            _horizontalVector = gride.HorizontalVector;
            _verticalVector   = gride.VerticalVector;
        }
Пример #3
0
        public static ObjectId AppendBlockItem(Point3d insertPoint, ObjectId blockTableRecordId,
                                               List <string> attrTextValues, Matrix3d toWcsTransform)
        {
            ObjectId resBlockId = ObjectId.Null;

            Tools.StartTransaction(() =>
            {
                Transaction trans = Tools.GetTopTransaction();

                // Add a block reference to the model space
                BlockTableRecord ms = Tools.GetAcadBlockTableRecordModelSpace(OpenMode.ForWrite);

                BlockTableRecord btr = blockTableRecordId.GetObjectForRead <BlockTableRecord>();

                BlockReference br = new BlockReference(insertPoint, blockTableRecordId);
                br.SetDatabaseDefaults();
                br.TransformBy(toWcsTransform);

                ObjectContextManager ocm    = btr.Database.ObjectContextManager;
                ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

                if (btr.Annotative == AnnotativeStates.True)
                {
                    br.AddContext(occ.CurrentContext);
                }

                resBlockId = ms.AppendEntity(br);
                trans.AddNewlyCreatedDBObject(br, true);

                // Add attributes from the block table record
                List <AttributeDefinition> attributes = GetAttributes(btr, trans);
                int i = 0;
                foreach (AttributeDefinition acAtt in attributes)
                {
                    if (!acAtt.Constant)
                    {
                        using (AttributeReference acAttRef = new AttributeReference())
                        {
                            acAttRef.SetAttributeFromBlock(acAtt, br.BlockTransform);

                            if (attrTextValues != null)
                            {
                                acAttRef.TextString = attrTextValues[i++];
                            }
                            else
                            {
                                acAttRef.TextString = acAtt.TextString;
                            }

                            br.AttributeCollection.AppendAttribute(acAttRef);
                            trans.AddNewlyCreatedDBObject(acAttRef, true);
                        }
                    }
                }
                br.RecordGraphicsModified(true);
            });
            return(resBlockId);
        }
Пример #4
0
        public void Scale()
        {
            Database db  = HostApplicationServices.WorkingDatabase;
            DBText   txt = DBText(Point3d.Origin, "深居浅出", 100);

            txt.Annotative = AnnotativeStates.True;
            ObjectContextManager    cm  = db.ObjectContextManager;
            ObjectContextCollection occ = cm.GetContextCollection("ACDB_ANNOTATIONSCALES");

            foreach (ObjectContext oc in occ)
            {
                txt.AddContext(oc);
            }
            Circle cir = new Circle(new Point3d(265, 50, 0), Vector3d.ZAxis, 300);

            ToModelSpace(txt);
            ToModelSpace(cir);
        }
Пример #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();//为了安全将实体切换为读的状态
            }
        }
Пример #6
0
        /// <summary>
        /// 为图形添加一个新的注释比例
        /// </summary>
        /// <param name="db">图形数据库对象</param>
        /// <param name="scaleName">缩放比例名</param>
        /// <param name="paperUnits">缩放比例的图纸单位</param>
        /// <param name="drawingUnits">缩放比例的图形单位</param>
        /// <returns>返回添加的注释比例</returns>
        public static AnnotationScale AddScale(this Database db, string scaleName, double paperUnits, double drawingUnits)
        {
            AnnotationScale scale = null;//声明一个注释比例对象
            //获取当前图形的对象比例管理器
            ObjectContextManager ocm = db.ObjectContextManager;
            //获取当前图形的注释比例列表,名为ACDB_ANNOTATIONSCALES
            ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

            //如果没有名为scaleName的注释比例
            if (!occ.HasContext(scaleName))
            {
                scale              = new AnnotationScale(); //新建一个注释比例对象
                scale.Name         = scaleName;             //注释比例名
                scale.PaperUnits   = paperUnits;            //注释比例的图纸单位
                scale.DrawingUnits = drawingUnits;          //注释比例的图形单位
                occ.AddContext(scale);                      //将scaleName注释比例添加比例列表中
            }
            return(scale);                                  //返回scaleName注释比例对象
        }
Пример #7
0
        private DBText _createDBText(Point3d alignmentPoint, double height)
        {
            ObjectContextManager    ocm   = HostApplicationServices.WorkingDatabase.ObjectContextManager;
            ObjectContextCollection occ   = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
            AnnotationScale         scale = (AnnotationScale)occ.CurrentContext;

            DBText text = new DBText();

            text.SetDatabaseDefaults();
            text.Height         = height; /* * scale.DrawingUnits/ scale.PaperUnits;*/
            text.Annotative     = AnnotativeStates.False;
            text.Rotation       = _textRotation;
            text.Position       = Point3d.Origin;
            text.HorizontalMode = TextHorizontalMode.TextMid;
            text.VerticalMode   = TextVerticalMode.TextVerticalMid;
            text.AlignmentPoint = alignmentPoint;
            //text.AddContext(occ.CurrentContext);
            text.AdjustAlignment(HostApplicationServices.WorkingDatabase);

            return(text);
        }
        private void SetStyleAnnotative(DimStyleTableRecord[] resultStyleName, DBObject item, Dimension y)
        {
            AnnotationScale      mainScale = new AnnotationScale();
            ObjectContextManager ocm       = targetDB.ObjectContextManager;
            double scale      = 2.5 / (y.Dimscale * y.Dimtxt);
            double difference = 200;

            if (ocm != null)
            {
                ObjectContextCollection occ            = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
                ObjectContext           currentContext = occ.CurrentContext;
                foreach (ObjectContext context in occ)
                {
                    double          currentDifference = 200;
                    AnnotationScale annotScale        = (AnnotationScale)context;
                    if (annotScale.Scale == scale)
                    {
                        mainScale = annotScale;
                        break;
                    }
                    else
                    {
                        currentDifference = Math.Abs(scale - annotScale.Scale);
                        if (currentDifference < difference)
                        {
                            difference = currentDifference;
                            mainScale  = annotScale;
                        }
                    }
                }
                SetStyleByType(resultStyleName, item, y);
                if (y.HasContext(currentContext))
                {
                    y.RemoveContext(currentContext);
                }
                y.AddContext(mainScale);
                y.Dimtxt = 2.5;
            }
        }
Пример #9
0
        /// <summary>
        /// 获取实体拥有的所有缩放比例
        /// </summary>
        /// <param name="entId">实体的Id</param>
        /// <returns>返回实体的缩放比例列表</returns>
        public static List <ObjectContext> GetAllScales(this ObjectId entId)
        {
            //声明一个列表对象,用于返回实体所拥有的所有的注释比例
            List <ObjectContext> scales = new List <ObjectContext>();
            DBObject             obj    = entId.GetObject(OpenMode.ForRead);

            if (obj.Annotative != AnnotativeStates.NotApplicable)
            {
                ObjectContextManager    ocm = obj.Database.ObjectContextManager;
                ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
                //遍历注释比例列表
                foreach (ObjectContext oc in occ)
                {
                    //如果实体拥有此注释比例
                    if (ObjectContexts.HasContext(obj, oc))
                    {
                        scales.Add(oc);//将此注释比例添加到返回列表中
                    }
                }
            }
            return(scales);//返回实体所拥有的所有的注释比例
        }
Пример #10
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);
        }
Пример #11
0
        public static ObjectId AddBlockRefToModelSpace(ObjectId blockTableRecordId,
                                                       List <string> attrTextValues, Point3d location, Matrix3d matrix, Transaction trans, bool commit)
        {
            // Add a block reference to the model space
            BlockTableRecord ms = Tools.GetAcadBlockTableRecordModelSpace(trans, OpenMode.ForWrite);

            BlockTableRecord btr = (BlockTableRecord)trans.GetObject(blockTableRecordId, OpenMode.ForRead);

            BlockReference br = new BlockReference(location, blockTableRecordId);

            br.TransformBy(matrix);

            ObjectContextManager    ocm = btr.Database.ObjectContextManager;
            ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

            if (btr.Annotative == AnnotativeStates.True)
            {
                br.AddContext(occ.CurrentContext);
            }

            //br.RecordGraphicsModified(true);

            ObjectId brId = ms.AppendEntity(br);

            trans.AddNewlyCreatedDBObject(br, true);



            // Add attributes from the block table record
            List <AttributeDefinition> attributes = GetAttributes(btr, trans);
            int i = 0;

            foreach (AttributeDefinition acAtt in attributes)
            {
                if (!acAtt.Constant)
                {
                    using (AttributeReference acAttRef = new AttributeReference())
                    {
                        //acAttRef.RecordGraphicsModified(true);

                        acAttRef.SetAttributeFromBlock(acAtt, br.BlockTransform);
                        //acAttRef.Position = acAtt.Position.TransformBy(br.BlockTransform);

                        if (attrTextValues != null)
                        {
                            acAttRef.TextString = attrTextValues[i++];
                        }
                        else
                        {
                            acAttRef.TextString = acAtt.TextString;
                        }

                        //if (acAtt.Annotative == AnnotativeStates.True)
                        //acAttRef.AddContext(occ.CurrentContext);

                        br.AttributeCollection.AppendAttribute(acAttRef);
                        trans.AddNewlyCreatedDBObject(acAttRef, true);
                    }
                }

                // Change the attribute definition to be displayed as backwards
                //acAtt.UpgradeOpen();
                //acAtt.IsMirroredInX = true;
                //acAtt.IsMirroredInY = false;
            }
            br.RecordGraphicsModified(true);
            if (commit)
            {
                trans.Commit();
            }
            return(brId);
        }
        public BlockReference GetObject(Point3d position, List <string> attrTextValues)
        {
            if (this._blockId != ObjectId.Null)
            {
                _createBlockRecord(_name, _annotativeState, _origin, false);
            }

            BlockReference br = null;

            Tools.StartTransaction(() =>
            {
                if (_blockRecordId != ObjectId.Null)
                {
                    BlockTable bt       = _db.BlockTableId.GetObjectForRead <BlockTable>();
                    BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObjectForWrite <BlockTableRecord>();

                    BlockTableRecord btr = _blockRecordId.GetObjectForRead <BlockTableRecord>();

                    br = new BlockReference(position, _blockRecordId);

                    ObjectContextManager ocm    = btr.Database.ObjectContextManager;
                    ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

                    if (btr.Annotative == AnnotativeStates.True)
                    {
                        br.AddContext(occ.CurrentContext);
                    }

                    ObjectId brId = ms.AppendEntity(br);
                    _db.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(br, true);

                    // Add attributes from the block table record
                    List <AttributeDefinition> attributes = btr.GetAttributes();
                    int i = 0;
                    foreach (AttributeDefinition acAtt in attributes)
                    {
                        if (!acAtt.Constant)
                        {
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                //acAttRef.RecordGraphicsModified(true);

                                acAttRef.SetAttributeFromBlock(acAtt, br.BlockTransform);
                                //acAttRef.Position = acAtt.Position.TransformBy(br.BlockTransform);

                                acAttRef.TextString = attrTextValues[i++];

                                //if (acAtt.Annotative == AnnotativeStates.True)
                                //acAttRef.AddContext(occ.CurrentContext);

                                br.AttributeCollection.AppendAttribute(acAttRef);
                                _db.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(acAttRef, true);
                            }
                        }
                    }
                    _insertPoint = position;
                    _blockId     = br.Id;
                }
            });
            return(br);
        }