码迷,mamicode.com
首页 > Web开发 > 详细

CAD.NET调整属性块中文字宽度

时间:2014-07-09 15:22:19      阅读:1189      评论:0      收藏:0      [点我收藏+]

标签:des   width   art   cti   for   io   

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(AttributeWidthFactor.MyCommands))]

namespace AttributeWidthFactor
{
    public class MyCommands
    {
        [CommandMethod("AttWidth")]
        public static void SetAttrWidthFactor()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            //Pick an attributereference in a block
            PromptNestedEntityOptions opt = new 
                PromptNestedEntityOptions("\nPick an attribute:");
            PromptNestedEntityResult res = ed.GetNestedEntity(opt);

            if (res.Status == PromptStatus.OK)
            {
                if (res.ObjectId.ObjectClass.DxfName.ToUpper() == "ATTRIB")
                {
                    //Ask user to pick a distance as desired width for 
                    //the attribute to fit in. Based on the block, the width 
                    //could be a known value
                    PromptPointOptions popt = new 
                        PromptPointOptions("\nPick width base point:");
                    PromptPointResult pres = ed.GetPoint(popt);
                    if (pres.Status != PromptStatus.OK) return;
                    Point3d basePt = pres.Value;

                    PromptDistanceOptions dopt = 
                        new PromptDistanceOptions("\nPick width: ");
                    dopt.UseBasePoint = true;
                    dopt.BasePoint = basePt;

                    PromptDoubleResult dres = ed.GetDistance(dopt);
                    if (dres.Status != PromptStatus.OK) return;

                    //This is the width we want to fit the attribute text‘s width
                    double w = dres.Value;

                    using (Transaction tran = 
                        dwg.TransactionManager.StartTransaction())
                    {
                        AttributeReference att = (AttributeReference)tran.GetObject(
                            res.ObjectId, OpenMode.ForWrite);

                        //Get attribute‘s width, assuming it is placed horizontally
                        double aw = Math.Abs(att.GeometricExtents.MaxPoint.X 
                            - att.GeometricExtents.MinPoint.X);

                        //This is the WidthFactor
                        double factor = w / aw;
                        att.WidthFactor = factor;

                        tran.Commit();
                    }
                }
                else
                {
                    Application.ShowAlertDialog("Not an attribute!");
                }
            }
        }
    }
}

CAD.NET调整属性块中文字宽度,布布扣,bubuko.com

CAD.NET调整属性块中文字宽度

标签:des   width   art   cti   for   io   

原文地址:http://www.cnblogs.com/swtool/p/3832332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!