25/11/2024

EntityJig Jig chèn block vào bản vẽ | EntityJig insert blockreference into the drawing | AutoCAD dotNet

Ứng dụng được phát triển bởi đội ngũ AutoLISP Thật là đơn giản

    

Thông tin thêm: 👉👉👉

1 Thêm class MyCommands.cs

Lưu mã sau dưới dạng tệp tin MyCommands.cs
Code:
// (C) Copyright 2024 by
//
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AJS_InsertBlockReference_EntityJig.MyCommands))]

namespace AJS_InsertBlockReference_EntityJig
{
    public class MyCommands
    {
        // Modal Command with localized name
        [CommandMethod("AJS_InsertBlock", CommandFlags.Modal)]
        public void Command_AJS_InsertBlock() // This method can have any name
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            if (doc == null) return;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            ed.WriteMessage("\nInsertBlockReference Jig - Edited by www.lisp.vn");

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                int n = 1;

                Dictionary<int, ObjectId> btrs = new Dictionary<int, ObjectId>();

                ed.WriteMessage("\nSelect order of a block list: ");
                foreach (var id in bt)
                {
                    var btr = tr.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
                    if (btr == null || btr.IsLayout || btr.IsAnonymous || string.IsNullOrEmpty(btr.Name)) continue;

                    btrs.Add(n, id);
                    ed.WriteMessage("\n" + n++ + "." + btr.Name);
                }

                PromptIntegerOptions pio = new PromptIntegerOptions("\nSpecify block order (from 1 to " + (n - 1) + ")");
                pio.AllowNegative = false;
                pio.AllowNone = false;

                var pir = ed.GetInteger(pio);
                if (pir.Status != PromptStatus.OK) return;

                if (pir.Value >= 1 && btrs.ContainsKey(pir.Value))
                {
                    var br = new BlockReference(Point3d.Origin, btrs[pir.Value]);
                    if (br != null)
                    {
                        if (BlockMovingRotating.Jig(br))
                        {
                            var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                            btr.AppendEntity(br);
                            tr.AddNewlyCreatedDBObject(br, true);

                            var blockdef = tr.GetObject(br.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                            foreach (var atid in blockdef)
                            {
                                var attDef = tr.GetObject(atid, OpenMode.ForRead) as AttributeDefinition;
                                if (attDef != null && !attDef.Constant)
                                {
                                    var atr = new AttributeReference();
                                    atr.SetAttributeFromBlock(attDef, br.BlockTransform);
                                    atr.TextString = atr.getTextWithFieldCodes().Replace("?BlockRefId", "%<\\_ObjId " + br.ObjectId.OldIdPtr.ToString() + ">%");
                                    br.AttributeCollection.AppendAttribute(atr);
                                    tr.AddNewlyCreatedDBObject(atr, true);
                                }
                            }
                        }
                    }
                }

                tr.Commit();
            }

            ed.WriteMessage("\nInsertBlockReference Jig - Edited by www.lisp.vn");
        }
    }
}

2 Thêm class BlockMovingRotating.cs

Lưu mã sau dưới dạng tệp tin BlockMovingRotating.lsp
Code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;

namespace AJS_InsertBlockReference_EntityJig
{
    public class BlockMovingRotating : EntityJig
    {
        #region Fields

        public int mCurJigFactorNumber = 1;

        private Point3d mPosition;    // Factor #1
        private double mRotation;    // Factor #2

        #endregion Fields

        #region Constructors

        public BlockMovingRotating(Entity ent) : base(ent)
        {
        }

        #endregion Constructors

        #region Overrides

        protected override bool Update()
        {
            switch (mCurJigFactorNumber)
            {
                case 1:
                    (Entity as BlockReference).Position = mPosition;
                    break;

                case 2:
                    (Entity as BlockReference).Rotation = mRotation;
                    break;

                default:
                    return false;
            }

            return true;
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            switch (mCurJigFactorNumber)
            {
                case 1:
                    JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nBlock position:");
                    PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
                    if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;

                    if (prResult1.Value.Equals(mPosition))
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        mPosition = prResult1.Value;
                        return SamplerStatus.OK;
                    }
                case 2:
                    JigPromptAngleOptions prOptions2 = new JigPromptAngleOptions("\nBlock rotation angle:");
                    prOptions2.BasePoint = (Entity as BlockReference).Position;
                    prOptions2.UseBasePoint = true;
                    PromptDoubleResult prResult2 = prompts.AcquireAngle(prOptions2);
                    if (prResult2.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;

                    if (prResult2.Value.Equals(mRotation))
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        mRotation = prResult2.Value;
                        return SamplerStatus.OK;
                    }
                default:
                    break;
            }

            return SamplerStatus.OK;
        }

        #endregion Overrides

        #region Method to Call

        public static bool Jig(BlockReference ent)
        {
            try
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
                BlockMovingRotating jigger = new BlockMovingRotating(ent);
                PromptResult pr;
                do
                {
                    pr = ed.Drag(jigger);
                } while (pr.Status != PromptStatus.Cancel &&
                            pr.Status != PromptStatus.Error &&
                            pr.Status != PromptStatus.Keyword &&
                            jigger.mCurJigFactorNumber++ <= 2);

                return pr.Status == PromptStatus.OK;
            }
            catch
            {
                return false;
            }
        }

        #endregion Method to Call
    }
}

Nguồn tham khảo



---------------------------------------------------------------------------------------------
Ứng dụng được phát triển bởi đội ngũ AutoLISP Thật là đơn giản - Tác giả ứng dụng in D2P

    

Mọi thông tin xin liên hệ Fanpage AutoLISP Thật là đơn giản!
Cảm ơn bạn đã theo dõi!

Không có nhận xét nào:

Đăng nhận xét

Hàm COUNTIFS đếm theo nhiều điều kiện trong Excel | COUNTIFS, COUNTIF | AutoLISP Reviewer

Hàm COUNTIFS đếm theo nhiều điều kiện trong Excel Cuối tháng cần biết phòng Kế toán có bao nhiêu người đi làm đủ công, bao nhiêu đơn hàng ...