EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

萧闫子 发表于 2014-1-8 14:56:46

[即时预览] 使用向导创建XLine Jig

AutoCAD .NET Addin Wizard (AcadNetAddinWizard) is far more than just some templates scattered around. It is a real-wizard-sense wizard, i.e. multi paged, configurable, flexible and intelligent. It also includes various project item wizards, coders, and widgets for AutoCAD .NET programming.In this article, let us see how the Entity Jigger wizard of AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can create a xline jigger automatically in no time for us.The Entity Jigger wizard can be found from the Add New Item UI of both Visual C# and VB.NET of Visual Studio both full-fledged and Express in version both 2008 and 2010. Let’s take the C# of Visual Studio 2010 for an example. The AutoCAD Addin category will appear under the Visual C# Items node of the Installed Templates and its various item wizards including the Entity Jigger wizard appear in turn in the middle pane:http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b016764b4a008970b-800wi

Here are wizard pages and some sample fill-outs of the Entity Jigger wizard for the coming Xline Jig:

http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b016764b4a0cf970b-800wi

http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b016764b4a125970b-800wi
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b016764b4a1c0970b-800wi
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b0168e9b5a8b1970c-800wi
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b016303c00180970d-800wi<div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">
The auto-generated Xline Jig class and test command may look like the following:</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">#region Namespaces</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">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.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.IO;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcWindowsNS = Autodesk.AutoCAD.Windows;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">#endregion</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">namespace Acad2012NetDemoAddinCS
{
    public class EntityJigger1 : EntityJig
    {
      #region Fields</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      public int mCurJigFactorIndex = 1;// Jig Factor Index</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      public Autodesk.AutoCAD.Geometry.Point3d mBasePoint; // Jig Factor #1
      public Autodesk.AutoCAD.Geometry.Point3d mSecondPoint; // Jig Factor #2</div>
<div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #endregion</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #region Constructors</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      public EntityJigger1(Xline ent)
            : base(ent)
      {
            // Initialize and transform the Entity.
            Entity.SetDatabaseDefaults();
            Entity.TransformBy(UCS);</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #endregion</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #region Properties</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      private Editor Editor
      {
            get
            {
                return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
            }
      }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      private Matrix3d UCS
      {
            get
            {
                return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
            }
      }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #endregion</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #region Overrides</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      public new Xline Entity// Overload the Entity property for convenience.
      {
            get
            {
                return base.Entity as Xline;
            }
      }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      protected override bool Update()
      {</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">            switch (mCurJigFactorIndex)
            {
                case 1:
                  Entity.BasePoint = mBasePoint;
                  //Entity.BasePoint.TransformBy(UCS); //Turn it on for UCS transformation or tweak it if not compile.
                  break;
                case 2:
                  Entity.SecondPoint = mSecondPoint;
                  //Entity.SecondPoint.TransformBy(UCS); //Turn it on for UCS transformation or tweak it if not compile.
                  break;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">                default:
                  return false;
            }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">            return true;
      }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      protected override SamplerStatus Sampler(JigPrompts prompts)
      {
            switch (mCurJigFactorIndex)
            {
                case 1:
                  JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nBase point::");
                  // Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
                  prOptions1.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect | UserInputControls.UseBasePointElevation;
                  PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
                  if (prResult1.Status == PromptStatus.Cancel && prResult1.Status == PromptStatus.Error)
                        return SamplerStatus.Cancel;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">                  if (prResult1.Value.Equals(mBasePoint))//Use better comparison method if necessary.
                  {
                        return SamplerStatus.NoChange;
                  }
                  else
                  {
                        mBasePoint = prResult1.Value;
                        return SamplerStatus.OK;
                  }
                case 2:
                  JigPromptPointOptions prOptions2 = new JigPromptPointOptions("\nSecond point:");
                  // Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
                  prOptions2.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect | UserInputControls.UseBasePointElevation;
                  PromptPointResult prResult2 = prompts.AcquirePoint(prOptions2);
                  if (prResult2.Status == PromptStatus.Cancel && prResult2.Status == PromptStatus.Error)
                        return SamplerStatus.Cancel;</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">                  if (prResult2.Value.Equals(mSecondPoint) || prResult2.Value.Equals(mBasePoint))
                  {
                        return SamplerStatus.NoChange;
                  }
                  else
                  {
                        mSecondPoint = prResult2.Value;
                        return SamplerStatus.OK;
                  }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">                default:
                  break;
            }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">            return SamplerStatus.OK;
      }</div>
<div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #endregion</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #region Methods to Call</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      public static Xline Jig()
      {
            EntityJigger1 jigger = null;
            try
            {
                jigger = new EntityJigger1(new Xline());
                PromptResult pr;
                do
                {
                  pr = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.Drag(jigger);
                  if (pr.Status == PromptStatus.Keyword)
                  {
                        // Keyword handling code</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">                  }
                  else
                  {
                        jigger.mCurJigFactorIndex++;
                  }
                } while (pr.Status != PromptStatus.Cancel && pr.Status != PromptStatus.Error && jigger.mCurJigFactorIndex <= 2);</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">                if (pr.Status == PromptStatus.Cancel || pr.Status == PromptStatus.Error)
                {
                  if (jigger != null && jigger.Entity != null)
                        jigger.Entity.Dispose();</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">                  return null;
                }
                else
                  return jigger.Entity;
            }
            catch
            {
                if (jigger != null && jigger.Entity != null)
                  jigger.Entity.Dispose();</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">                return null;
            }
      }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #endregion</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #region Test Commands</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      
      public static void TestEntityJigger1_Method()
      {
            try
            {
                Entity jigEnt = EntityJigger1.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());
            }
      }</div><div align="left" style="color: rgb(105, 105, 105); font-family: 'Microsoft Yahei', Simsun; ">      #endregion
    }
}</div>The only code change needs to be made manually is to check if the second point equals to the base point and do nothing if true. Apart from this tiny point, the auto-generated code works well, including taking care of user cancelations and honoring the current UCS. The following demonstrates how it behaves in AutoCAD.http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b0168e9b5aae8970c-800wi
The Entity Jigger wizard of the leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard)can help you create EntityJig derivatives for various entity types automatically, quickly, reliably and professionally.
页: [1]
查看完整版本: [即时预览] 使用向导创建XLine Jig