明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: qq1254582201

【转载】CAD多重引线相关内容转载合集

[复制链接]
 楼主| 发表于 6 天前 | 显示全部楼层
AutoCAD .NET: MultiLeader (MLeader) Jig 4 – Jigging Block with Attribute
We provided various jig implementations before, either using EntityJig or DrawJig, either from scratch or with the assistance of the Entity Jigger or Draw Jigger of AcadNetAddinWizard(Pro).
We demonstrated how to jig a MultiLeader programmatically through an arrow location and a single landing location using AutoCAD .NET and C#. We also showed how to jig multiple landing locations. We presented last time how to jig block content instead of the default MText content. In this post, we are going to extend the jig to support any blocks having attributes defined. This will make the MLeaderJigger more flexible and useful since the block content will likely change from one case to another in the real design world.
Here we go.
#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

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

using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcWindowsNS = Autodesk.AutoCAD.Windows;

#endregion

namespace AcadNetCSharp
{
    public class MLeaderJigger4 : EntityJig
    {
        #region Fields

        private const string BlockName = "tagAtt";
        public static string TagContent = "";

        public int mCurJigFactorIndex = 1;  // Jig Factor Index

        public Autodesk.AutoCAD.Geometry.Point3d mArrowLocation; // Jig Factor #1
        public Autodesk.AutoCAD.Geometry.Point3d mLandingLocation; // Jig Factor #2

        #endregion

        #region Constructors

        public MLeaderJigger4(MLeader ent)
            : base(ent)
        {
            Entity.SetDatabaseDefaults();

            Entity.ContentType = ContentType.BlockContent;

            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                Entity.BlockContentId = bt[BlockName];
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(Entity.BlockContentId, OpenMode.ForWrite);
                if (btr.HasAttributeDefinitions)
                {
                    foreach (ObjectId id in btr)
                    {
                        DBObject obj = tr.GetObject(id, OpenMode.ForRead);
                        if (obj is AttributeDefinition)
                        {
                            AttributeDefinition ad = obj as AttributeDefinition;
                            AttributeReference ar = new AttributeReference();
                            ar.SetAttributeFromBlock(ad, Matrix3d.Displacement(Entity.BlockPosition - Point3d.Origin));
                            ar.TextString = TagContent;

                            Entity.SetBlockAttribute(id, ar);
                        }
                    }
                }

                tr.Commit();
            }

            Entity.EnableDogleg = true;
            Entity.EnableLanding = true;
            Entity.EnableFrameText = false;

            Entity.AddLeaderLine(mLandingLocation);
            Entity.SetFirstVertex(0, mArrowLocation);

            Entity.TransformBy(UCS);
        }

        #endregion

        #region Properties

        private Editor Editor
        {
            get
            {
                return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
            }
        }

        private Matrix3d UCS
        {
            get
            {
                return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
            }
        }

        #endregion

        #region Overrides

        public new MLeader Entity  // Overload the Entity property for convenience.
        {
            get
            {
                return base.Entity as MLeader;
            }
        }

        protected override bool Update()
        {

            switch (mCurJigFactorIndex)
            {
                case 1:
                    Entity.SetFirstVertex(0, mArrowLocation);
                    Entity.SetLastVertex(0, mArrowLocation);

                    break;
                case 2:
                    Entity.SetLastVertex(0, mLandingLocation);

                    break;

                default:
                    return false;
            }

            return true;
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            switch (mCurJigFactorIndex)
            {
                case 1:
                    JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nArrow Location:");
                    // Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
                    prOptions1.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByOrthoMode | UserInputControls.GovernedByUCSDetect | UserInputControls.UseBasePointElevation;
                    PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
                    if (prResult1.Status == PromptStatus.Cancel && prResult1.Status == PromptStatus.Error)
                        return SamplerStatus.Cancel;

                    if (prResult1.Value.Equals(mArrowLocation))  //Use better comparison method if necessary.
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        mArrowLocation = prResult1.Value;
                        return SamplerStatus.OK;
                    }
                case 2:
                    JigPromptPointOptions prOptions2 = new JigPromptPointOptions("\nLanding Location:");
                    // Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
                    prOptions2.UseBasePoint = true;
                    prOptions2.BasePoint = mArrowLocation;
                    prOptions2.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByOrthoMode | UserInputControls.GovernedByUCSDetect | UserInputControls.UseBasePointElevation;
                    PromptPointResult prResult2 = prompts.AcquirePoint(prOptions2);
                    if (prResult2.Status == PromptStatus.Cancel && prResult2.Status == PromptStatus.Error)
                        return SamplerStatus.Cancel;

                    if (prResult2.Value.Equals(mLandingLocation))  //Use better comparison method if necessary.
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        mLandingLocation = prResult2.Value;
                        return SamplerStatus.OK;
                    }

                default:
                    break;
            }

            return SamplerStatus.OK;
        }



        #endregion

        #region Methods to Call

        public static MLeader Jig()
        {
            MLeaderJigger4 jigger = null;
            try
            {
                jigger = new MLeaderJigger4(new MLeader());
                PromptResult pr;
                do
                {
                    pr = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.Drag(jigger);
                    if (pr.Status == PromptStatus.Keyword)
                    {
                        // Keyword handling code

                    }
                    else
                    {
                        jigger.mCurJigFactorIndex++;
                    }
                } while (pr.Status != PromptStatus.Cancel && pr.Status != PromptStatus.Error && jigger.mCurJigFactorIndex <= 2);

                if (pr.Status == PromptStatus.Cancel || pr.Status == PromptStatus.Error)
                {
                    if (jigger != null && jigger.Entity != null)
                        jigger.Entity.Dispose();

                    return null;
                }
            
                return jigger.Entity;
            }
            catch(System.Exception ex)
            {
                MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());

                if (jigger != null && jigger.Entity != null)
                    jigger.Entity.Dispose();

                return null;
            }
        }

        #endregion

        #region Test Commands

        [CommandMethod("TestMLeaderJigger4")]
        public static void TestMLeaderJigger4_Method()
        {
            try
            {
                TagContent = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.GetString("\nTag: ").StringResult;
                Entity jigEnt = MLeaderJigger4.Jig();
                if (jigEnt != null)
                {
                    Database db = HostApplicationServices.WorkingDatabase;
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        btr.AppendEntity(jigEnt);
                        tr.AddNewlyCreatedDBObject(jigEnt, true);
                        tr.Commit();
                    }
                }
            }
            catch (System.Exception ex)
            {
                MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
            }
        }

        #endregion
    }
}

The workflow and output may look like the following:


As can be seen, the block attribute value (tag) is collected first so that the tag with the exact string value can be jigged nicely along with the arrow, leader line, and standing line. The UCS is still perfectly honored. Before running the jig test code, please make sure a block named “tagAtt” exists and it has at least a block attribute defined. The block attribute can have any tag name. If multiple block attributes defined, all of them will have the value as input at the beginning of the jigging.



回复 支持 反对

使用道具 举报

发表于 5 天前 | 显示全部楼层
用vlisp可以轻松定义和修改mleader的文字内容。
个人好奇的是:vb.net开发插件,可以实现非模态窗口,和动态显示编辑里的功能(如移动旋转吗)
vb net的学习容易吗?
回复 支持 反对

使用道具 举报

发表于 5 天前 | 显示全部楼层
qq1254582201 发表于 2025-4-16 17:08
AutoCAD .NET: MultiLeader (MLeader) Jig 4 – Jigging Block with AttributeWe provided various jig imp ...

这篇有很多误导人的错误代码
回复 支持 反对

使用道具 举报

发表于 5 天前 | 显示全部楼层
sdh123321 发表于 2025-4-17 08:59
用vlisp可以轻松定义和修改mleader的文字内容。
个人好奇的是:vb.net开发插件,可以实现非模态窗口,和动 ...

学C#吧,不然你享受不到IFox和我的博客,
.NET肯定都可以实现非模态窗口,你的问题都不是什么难题
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-4-22 01:46 , Processed in 0.153236 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表