Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - (gile)

Pages: 1 ... 4 5 [6]
76
F# language / Re: Code Converter for F#
« on: November 24, 2010, 08:10:10 AM »
Hi

Never seen one.
F# is really different from C# or VB.net (which are very closed) and it's quite new.

One way to convert should be to get the exe or dll compiled from F# code in CIL and diassemble it with Reflector. But, according to what I tryied the result isn't so good.

77
Blocks / Re: Erase doubled block references
« on: November 23, 2010, 11:15:13 PM »
The Duplicate method can be defined inside the class too, as a private method with 2 arguments.

This is the way used in this F# implementation.

Code: [Select]
module DupBlks

open System
open System.Collections.Generic
open Autodesk.AutoCAD.ApplicationServices
open Autodesk.AutoCAD.DatabaseServices
open Autodesk.AutoCAD.EditorInput
open Autodesk.AutoCAD.Geometry
open Autodesk.AutoCAD.Runtime

let Duplicate (a: BlockReference) (b: BlockReference) =
    let tol = new Tolerance(1e-6, 1e-6)
    a.Name = b.Name &&
    a.OwnerId = b.OwnerId &&
    a.Position = b.Position &&
    a.Layer = b.Layer &&
    Math.Round(a.Rotation, 5) = Math.Round(b.Rotation, 5) &&
    a.ScaleFactors = b.ScaleFactors

let DeleteDuplicatedBlockRef() =
    let db = HostApplicationServices.WorkingDatabase
    use tr = db.TransactionManager.StartTransaction()
   
    let getRefs (bt: BlockTable) =
        let mutable lst = []
        for id in bt do
            let btr = tr.GetObject(id, OpenMode.ForRead) :?> BlockTableRecord
            for oId in btr.GetBlockReferenceIds(true, false) do
                lst <- tr.GetObject(oId, OpenMode.ForWrite) :?> BlockReference :: lst
        lst

    let rec findDups acc blks =
        match blks with
        | [] -> acc
        | h :: t -> let pair = List.partition (fun x -> Duplicate x h) t
                    findDups (acc @ (fst pair)) (snd pair)
    let delBlk lst =
        List.iter (fun (x: BlockReference) -> x.Erase()) lst
        lst.Length
               
    let cnt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) :?> BlockTable
              |> getRefs |> findDups [] |> delBlk
    tr.Commit()
    cnt
       
[<CommandMethod("Test")>]
let Test() =
    try
        let del = DeleteDuplicatedBlockRef()
        Application.ShowAlertDialog(del.ToString() + " duplicated block(s) have been erased")
    with
        |ex -> Application.ShowAlertDialog(ex.Message)

78
Blocks / Re: Erase doubled block references
« on: November 23, 2010, 12:00:19 PM »
Hi,

By my side, I'd rather use a 'Duplicate' extension method for the BlockReference class than create a strucure.

Here's an example, in a 'BlockRefExtension' static class, two extension methods are defined to be used in the DeletDuplicatedBlockRef() method (a Test command is provided to test the code).
The GetName() method is needed while targeting the 2007 SDK (and perhaps 2088 and 2009) which doesn't contains the BlockReference.Name property.

EDIT: the DeletDuplicatedBlockRef() method returns the number of erased blocks
EDIT: removed some unusefull statements

Code: [Select]
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace DuplicateTest
{
    public static class BlockRefExtension
    {
        public static bool Duplicate(this BlockReference blk1, BlockReference blk2)
        {
            Tolerance tol = new Tolerance(1e-6, 1e-6);
            return
                blk1.GetName() == blk2.GetName() &&
                blk1.Layer == blk2.Layer &&
                Math.Round(blk1.Rotation, 5) == Math.Round(blk2.Rotation, 5) &&
                blk1.Position.IsEqualTo(blk2.Position, tol) &&
                blk1.ScaleFactors.IsEqualTo(blk2.ScaleFactors, tol);
        }

        public static string GetName(this BlockReference br)
        {
            using (Transaction tr = br.Database.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
                return btr.Name;
            }
        }
    }
    public class Commands
    {
        [CommandMethod("Test")]
        public void DelDupBlkRef()
        {
            try
            {
                int del = DeleteDuplicatedBlockRef();
                Application.ShowAlertDialog(del.ToString() + " duplicated block(s) have been erased");
            }
            catch (System.Exception e)
            {
                Application.ShowAlertDialog("\nError: " + e.Message);
            }
        }

        private int DeleteDuplicatedBlockRef()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            int result = 0;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId id in bt)
                {
                    BlockTableRecord btr =
                        (BlockTableRecord)tr.GetObject(id, OpenMode.ForWrite);
                    ObjectIdCollection idCol = btr.GetBlockReferenceIds(true, false);
                    for (int i = 0; i < idCol.Count; i++)
                    {
                        BlockReference blkRef =
                            (BlockReference)tr.GetObject(idCol[i], OpenMode.ForRead);
                        for (int j = i + 1; j < idCol.Count; j++)
                        {
                            BlockReference br =
                                (BlockReference)tr.GetObject(idCol[j], OpenMode.ForRead);
                            if (br.OwnerId == blkRef.OwnerId && br.Duplicate(blkRef))
                            {
                                idCol.RemoveAt(j);
                                br.UpgradeOpen();
                                br.Erase();
                                j--;
                                result++;
                            }
                        }
                    }
                }
                tr.Commit();
            }
            return result;
        }
    }
}

79
Layouts and printing / Re: Create and copy Layouts?
« on: November 23, 2010, 08:59:26 AM »
Hi,

Here's a little example to ceate a new layout with a viewport (maximum size according to paper margins).

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace LayoutAndViewportSample
{
    public class CreateVpCommand
    {
        [CommandMethod("Test")]
        public void Test()
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
           
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    DBDictionary layouts = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite);

                    // Get a layout name from the user
                    PromptStringOptions pso = new PromptStringOptions("\nEnter the layout name: ");
                    pso.AllowSpaces = true;
                    string name;
                    do
                    {
                        PromptResult pr = ed.GetString(pso);
                        if (pr.Status != PromptStatus.OK)
                            return;
                        name = pr.StringResult;
                    }
                    while (layouts.Contains(name));

                    // Create and activate the new layout
                    ObjectId layoutId = LayoutManager.Current.CreateLayout(name);
                    LayoutManager.Current.CurrentLayout = name;

                    // Calculate the maximum viewport size
                    Layout lay = (Layout)tr.GetObject(layoutId, OpenMode.ForWrite);
                    Point2d
                        minPt = lay.PlotPaperMargins.MinPoint,
                        maxPt = lay.PlotPaperMargins.MaxPoint,
                        pSize = lay.PlotPaperSize;
                    double width, height;
                    if ((int)lay.PlotRotation % 2 == 0)
                    {
                        width = pSize.X - maxPt.X - minPt.X;
                        height = pSize.Y - maxPt.Y - minPt.Y;
                    }
                    else
                    {
                        width = pSize.Y - maxPt.Y - minPt.Y;
                        height = pSize.X - maxPt.X - minPt.X;
                    }

                    // add the viewport to the layout BlockTableRecord
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite);
                    Viewport vp = new Viewport();
                    vp.Width = width;
                    vp.Height = height;
                    vp.CenterPoint = new Point3d(width / 2.0, height / 2.0, 0.0);
                    btr.AppendEntity(vp);
                    tr.AddNewlyCreatedDBObject(vp, true);
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: " + ex.Message);
            }
        }
    }
}

target audience:{beginner}

80
C# language / Re: Code conversion from C# to VB
« on: November 22, 2010, 09:22:11 AM »
Hi,

This one seems to be quite good (but none is perfect).

81
Windows forms / Osnap Palette
« on: November 21, 2010, 11:00:48 PM »
Hi,

The _osnap dialog box as a palette



The 'OsPalette' class where the OSPALETTE command (to show the palette) is defined.
Code: [Select]
    using System;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.Windows;

    namespace OsnapPalette
    {
        public class OsPalette
        {
            static PaletteSet ps;
            internal static OsmodeControl osmCtrl;

            public OsPalette()
            {
                osmCtrl = new OsmodeControl();
            }

            public void Show()
            {
                if (ps == null)
                {
                    ps = new PaletteSet("OSPALETTE", new Guid("{379AD059-A26C-4a3b-A05C-313A799D48CA}"));
                    ps.Style =
                      PaletteSetStyles.ShowPropertiesMenu |
                      PaletteSetStyles.ShowAutoHideButton |
                      PaletteSetStyles.ShowCloseButton;
                    ps.Name = "Accrochages aux objets";
                    ps.MinimumSize = new System.Drawing.Size(300, 340);
                    ps.Add("Accrobj", osmCtrl);
                }
                ps.Visible = true;
            }

            [CommandMethod("OSPALETTE")]
            public void Accobj()
            {
                this.Show();
            }
        }
    }

The 'OsmodeControl' class
Code: [Select]
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Autodesk.AutoCAD.ApplicationServices;
    using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
    using AcAs = Autodesk.AutoCAD.ApplicationServices;

    namespace OsnapPalette
    {
        public partial class OsmodeControl : UserControl
        {
            private Dictionary<int, CheckBox> modes = new Dictionary<int, CheckBox>();

            public OsmodeControl()
            {
                InitializeComponent();
                modes.Add(1, this.chkEnd);
                modes.Add(2, this.chkMid);
                modes.Add(4, this.chkCen);
                modes.Add(8, this.chkNod);
                modes.Add(16, this.chkQua);
                modes.Add(32, this.chkInt);
                modes.Add(64, this.chkIns);
                modes.Add(128, this.chkPer);
                modes.Add(256, this.chkTan);
                modes.Add(512, this.chkNea);
                modes.Add(2048, this.chkPrj);
                modes.Add(4096, this.chkExt);
                modes.Add(8192, this.chkPar);
                InitCheckBoxes();
                AcAp.SystemVariableChanged +=
                    new AcAs.SystemVariableChangedEventHandler(AcAp_SysvarChanged);
            }

            void AcAp_SysvarChanged(object sender, AcAs.SystemVariableChangedEventArgs e)
            {
                if (e.Name == "OSMODE" || e.Name == "AUTOSNAP")
                    InitCheckBoxes();
            }

            private void InitCheckBoxes()
            {
                short osmode = (short)AcAp.GetSystemVariable("OSMODE");
                foreach (KeyValuePair<int, CheckBox> pair in modes)
                {
                    pair.Value.Checked = (osmode & pair.Key) == pair.Key;
                }
                this.chkF3.Checked = (osmode & 16384) == 0;
                this.chkF11.Checked = (((short)AcAp.GetSystemVariable("AUTOSNAP")) & 16) == 16;
            }

            private void cmdAll_Click(object sender, EventArgs e)
            {
                foreach (KeyValuePair<int,CheckBox> pair in modes)
                {
                    pair.Value.Checked = true;
                }
            }

            private void cmdNone_Click(object sender, EventArgs e)
            {
                foreach (KeyValuePair<int, CheckBox> pair in modes)
                {
                    pair.Value.Checked = false;
                }
            }

            private void OsmodeControl_MouseLeave(object sender, EventArgs e)
            {
                Document doc = AcAp.DocumentManager.MdiActiveDocument;
                int autosnap = (short)AcAp.GetSystemVariable("AUTOSNAP");
                autosnap = chkF11.Checked ? autosnap | 16 : autosnap - (autosnap & 16);
                int osmode = 0;
                foreach (KeyValuePair<int,CheckBox> pair in modes)
                {
                    if (pair.Value.Checked)
                        osmode += pair.Key;
                }
                if (!chkF3.Checked)
                    osmode += 16384;
                using (DocumentLock docLock = doc.LockDocument())
                {
                    AcAp.SetSystemVariable("OSMODE", osmode);
                    AcAp.SetSystemVariable("AUTOSNAP", autosnap);
                }
            }
        }
    }

target audience:{intermediate}

82
Polylines / Add vertex
« on: November 21, 2010, 10:43:47 PM »
Hi,

The AVX command allows to add vertices to a polyline.
The `Jig' allows a dynamic preview of the new segment(s).
The command works whatever the polyline construction plane (`dynamic UCS' behavior while the polyline plane is different from the current UCS).
A DVX command allows to remove vertices

It deals with osnaps...


It deals with non constant widthes...


It deals with arc segments and with start and end points.


Code: [Select]
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace PolylineEdit
{
    public class AddVertex
    {
        Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
        Database db = HostApplicationServices.WorkingDatabase;

        class VertexJig : EntityJig
        {
            Polyline m_pline;
            Point3d m_point;
            int m_index;
            Vector3d m_vector;
            double m_bulge;
            double m_sWidth;
            double m_eWidth;

            public VertexJig(Polyline pline, Point3d point, int index, Vector3d vector, double bulge, double sWidth, double eWidth)
                : base(pline)
            {
                m_pline = pline;
                m_point = point;
                m_index = index;
                m_vector = vector;
                m_bulge = bulge;
                m_sWidth = sWidth;
                m_eWidth = eWidth;
            }

            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                JigPromptPointOptions jppo = new JigPromptPointOptions("\nSpecify the new vertex: ");
                jppo.UserInputControls = (UserInputControls.Accept3dCoordinates);
                PromptPointResult ppr = prompts.AcquirePoint(jppo);
                if (ppr.Status == PromptStatus.OK)
                {
                    if (ppr.Value.IsEqualTo(m_point))
                        return SamplerStatus.NoChange;
                    else
                    {
                        m_point = ppr.Value;
                        return SamplerStatus.OK;
                    }
                }
                return SamplerStatus.Cancel;
            }

            protected override bool Update()
            {
                if (m_pline.NumberOfVertices == 3)
                {
                    Point3d transPt = m_point.TransformBy(Matrix3d.WorldToPlane(m_pline.Normal));
                    Point2d pt = new Point2d(transPt.X, transPt.Y);
                    double length = m_pline.GetDistanceAtParameter(2);
                    double dist1 = m_pline.GetDistanceAtParameter(1);
                    double dist2 = length - dist1;
                    double width = m_sWidth < m_eWidth ?
                        ((dist1 * (m_eWidth - m_sWidth)) / length) + m_sWidth :
                        ((dist2 * (m_sWidth - m_eWidth)) / length) + m_eWidth;
                    double angle = Math.Atan(m_bulge);
                    m_pline.SetPointAt(m_index, pt);
                    m_pline.SetEndWidthAt(0, width);
                    m_pline.SetStartWidthAt(1, width);
                    m_pline.SetBulgeAt(0, Math.Tan(angle * (dist1 / length)));
                    m_pline.SetBulgeAt(1, Math.Tan(angle * (dist2 / length)));
                }
                else if (m_index == 0)
                {
                    Point3d transPt = m_point.TransformBy(Matrix3d.WorldToPlane(m_pline.Normal));
                    Point2d pt = new Point2d(transPt.X, transPt.Y);
                    m_pline.SetPointAt(m_index, pt);
                    if (m_bulge != 0.0)
                    {
                        Vector3d vec = m_point.GetVectorTo(m_pline.GetPoint3dAt(1));
                        double ang = vec.GetAngleTo(m_vector, m_pline.Normal);
                        double bulge = Math.Tan(ang / 2.0);
                        m_pline.SetBulgeAt(0, bulge);
                    }
                }
                else
                {
                    Point3d transPt = m_point.TransformBy(Matrix3d.WorldToPlane(m_pline.Normal));
                    Point2d pt = new Point2d(transPt.X, transPt.Y);
                    m_pline.SetPointAt(m_index, pt);
                    if (m_bulge != 0.0)
                    {
                        Vector3d vec = m_pline.GetPoint3dAt(0).GetVectorTo(m_point);
                        double ang = m_vector.GetAngleTo(vec, m_pline.Normal);
                        double bulge = Math.Tan(ang / 2.0);
                        m_pline.SetBulgeAt(0, bulge);
                    }
                }
                return true;
            }

            public Point3d GetPoint()
            {
                return m_point;
            }
        }

        [CommandMethod("AVX")]
        public void Avx()
        {
            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a segment where to add a vertex: ");
            peo.SetRejectMessage("\nIncorrect entity");
            peo.AllowNone = false;
            peo.AllowObjectOnLockedLayer = false;
            peo.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                Matrix3d UCS = ed.CurrentUserCoordinateSystem;
                ObjectId objId = per.ObjectId;
                try
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        Polyline pline = tr.GetObject(objId, OpenMode.ForRead, false) as Polyline;
                        if (pline != null)
                        {
                            Point3d pickPt = pickPointOnPline(pline, per.PickedPoint);
                            double param = pline.GetParameterAtPoint(pickPt);
                            int index = (int)param;

                            Matrix3d OCS = Matrix3d.PlaneToWorld(pline.Normal);
                            Point3d transPt = pickPt.TransformBy(OCS);

                            if (!OCS.CoordinateSystem3d.Zaxis.IsEqualTo(UCS.CoordinateSystem3d.Zaxis))
                            {
                                ed.CurrentUserCoordinateSystem = plineUCS(pline, index);
                            }

                            Int16 aperture = (Int16)acadApp.GetSystemVariable("APERTURE");
                            double viewsize = (double)acadApp.GetSystemVariable("VIEWSIZE");
                            Point2d screensize = (Point2d)acadApp.GetSystemVariable("SCREENSIZE");
                            double tol = 2 * aperture * viewsize / screensize.Y;
                            Tolerance tolerance = new Tolerance(tol, tol);

                            int endParam = pline.Closed == true ? pline.NumberOfVertices : pline.NumberOfVertices - 1;
                            Vector3d vec;

                            using (Polyline ghost = new Polyline())
                            {
                                ghost.ColorIndex = 7;

                                if ((pline.Closed == false) && pickPt.IsEqualTo(pline.GetPoint3dAt(0), tolerance))
                                {
                                    vec = pline.GetFirstDerivative(0);
                                    double bulge = pline.GetBulgeAt(0);
                                    double width = pline.GetStartWidthAt(0);
                                    Point2d p0 = new Point2d(transPt.X, transPt.Y);
                                    Point2d p1 = pline.GetPoint2dAt(0);

                                    ghost.AddVertexAt(0, p0, bulge, width, width);
                                    ghost.AddVertexAt(1, p1, bulge, width, width);
                                    ghost.Normal = pline.Normal;
                                    ghost.Elevation = pline.Elevation;

                                    VertexJig jig = new VertexJig(ghost, pickPt, 0, vec, bulge, width, width);

                                    PromptResult res = ed.Drag(jig);
                                    if (res.Status == PromptStatus.OK)
                                    {
                                        pline.UpgradeOpen();
                                        pline.AddVertexAt(index, ghost.GetPoint2dAt(0), ghost.GetBulgeAt(0), width, width);
                                    }
                                }
                                else if ((pline.Closed == false) && pickPt.IsEqualTo(pline.GetPoint3dAt(endParam), tolerance))
                                {
                                    vec = pline.GetFirstDerivative(endParam);
                                    double bulge = pline.GetBulgeAt(index);
                                    double width = pline.GetEndWidthAt(endParam);
                                    Point2d p0 = pline.GetPoint2dAt(endParam);
                                    Point2d p1 = new Point2d(transPt.X, transPt.Y);

                                    ghost.AddVertexAt(0, p0, bulge, width, width);
                                    ghost.AddVertexAt(1, p1, bulge, width, width);
                                    ghost.Normal = pline.Normal;
                                    ghost.Elevation = pline.Elevation;

                                    VertexJig jig = new VertexJig(ghost, pickPt, 1, vec, bulge, width, width);

                                    PromptResult res = ed.Drag(jig);
                                    if (res.Status == PromptStatus.OK)
                                    {
                                        pline.UpgradeOpen();
                                        pline.AddVertexAt(endParam + 1, ghost.GetPoint2dAt(1), ghost.GetBulgeAt(0), width, width);
                                        pline.SetBulgeAt(endParam, ghost.GetBulgeAt(0));
                                    }
                                }

                                else
                                {
                                    double bulge = pline.GetBulgeAt(index);
                                    double sWidth = pline.GetStartWidthAt(index);
                                    double eWidth = pline.GetEndWidthAt(index);
                                    Point2d p0 = pline.GetPoint2dAt(index);
                                    Point2d p1 = new Point2d(transPt.X, transPt.Y);
                                    Point2d p2;
                                    if (pline.Closed == false)
                                        p2 = pline.GetPoint2dAt(index + 1);
                                    else
                                    {
                                        try { p2 = pline.GetPoint2dAt(index + 1); }
                                        catch { p2 = pline.GetPoint2dAt(0); }
                                    }

                                    ghost.AddVertexAt(0, p0, bulge, sWidth, 0.0);
                                    ghost.AddVertexAt(1, p1, bulge, 0.0, eWidth);
                                    ghost.AddVertexAt(2, p2, 0.0, 0.0, 0.0);
                                    ghost.Normal = pline.Normal;
                                    ghost.Elevation = pline.Elevation;

                                    VertexJig jig = new VertexJig(ghost, pickPt, 1, vec, bulge, sWidth, eWidth);

                                    PromptResult res = ed.Drag(jig);
                                    if (res.Status == PromptStatus.OK)
                                    {
                                        pline.UpgradeOpen();
                                        pline.SetEndWidthAt(index, ghost.GetStartWidthAt(1));
                                        pline.AddVertexAt(index + 1, ghost.GetPoint2dAt(1), ghost.GetBulgeAt(1), ghost.GetStartWidthAt(1), eWidth);
                                        pline.SetBulgeAt(index, ghost.GetBulgeAt(0));
                                    }
                                }
                            }
                            ed.CurrentUserCoordinateSystem = UCS;
                        }
                        tr.Commit();
                    }
                }
                catch (System.Exception ex)
                {
                    ed.CurrentUserCoordinateSystem = UCS;
                    ed.WriteMessage("Error: " + ex.Message);
                }
            }
        }

        [CommandMethod("DVX")]
        public void Dvx()
        {
            PromptEntityOptions peo = new PromptEntityOptions("\nSelact the vertex to remove: ");
            peo.SetRejectMessage("\nIncorrect entity");
            peo.AllowNone = false;
            peo.AllowObjectOnLockedLayer = false;
            peo.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                ObjectId objId = per.ObjectId;
                try
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        Polyline pline = tr.GetObject(objId, OpenMode.ForWrite, false) as Polyline;
                        if (pline != null)
                        {
                            if (pline.NumberOfVertices > 2)
                            {
                                Point3d pickPt = pickPointOnPline(pline, per.PickedPoint);
                                double param = pline.GetParameterAtPoint(pickPt);
                                int index = (int)param;
                                if ((param - Math.Truncate(param)) > 0.5)
                                    index += 1;
                                pline.RemoveVertexAt(index);
                            }
                            else
                                ed.WriteMessage("\nOnly two vertices left");
                        }
                        tr.Commit();
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("Error: " + ex.Message);
                }
            }
        }

        private Point3d pickPointOnPline(Polyline pl, Point3d pt)
        {
            pt = pt.TransformBy(ed.CurrentUserCoordinateSystem);
            Vector3d vdir = ed.GetCurrentView().ViewDirection;
            pt = pt.Project(pl.GetPlane(), vdir);
            return pl.GetClosestPointTo(pt, false);
        }

        private Matrix3d plineUCS(Polyline pline, int param)
        {
            Point3d origin = pline.GetPoint3dAt(param);
            Vector3d xDir = origin.GetVectorTo(pline.GetPoint3dAt(param + 1)).GetNormal();
            Vector3d zDir = pline.Normal;
            Vector3d yDir = zDir.CrossProduct(xDir).GetNormal();
            return new Matrix3d(new double[16]{
                xDir.X, yDir.X, zDir.X, origin.X,
                xDir.Y, yDir.Y, zDir.Y, origin.Y,
                xDir.Z, yDir.Z, zDir.Z, origin.Z,
                0, 0, 0, 1});
        }
    }
}

target audience:{advanced}

83
Text / Strip Mtext
« on: November 21, 2010, 09:57:29 PM »
Hi,

This code was inspired by a Tony Tanzillo's one using the Mtext.ExplodeFragments() method to remove all Mtext formatting.

A command: STRIPMT to unformat all selected Mtexts
A LISP function: gc-GetStrippedMtextString to return the unformated text string of the mtext which ename is passed as argument to the function

Code: [Select]
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace StripMtextSample
{
    public class StripMtext
    {
        private StringBuilder m_sb = null;

        private string GetStrippedMtextContents(MText mt)
        {
            m_sb = new StringBuilder();
            mt.ExplodeFragments(new MTextFragmentCallback(FragmentCallback));
            return m_sb.ToString();
        }

        private MTextFragmentCallbackStatus FragmentCallback(MTextFragment fragment, object obj)
        {
            m_sb.Append(fragment.Text);
            return MTextFragmentCallbackStatus.Continue;
        }

        [CommandMethod("STRIPMT", CommandFlags.Modal | CommandFlags.UsePickSet)]
        public void StripSelSet()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "MTEXT") });
            ed.WriteMessage("\nSelect mtexts (or type 'Enter' for 'all').");
            PromptSelectionResult psr = ed.GetSelection(filter);
            if (psr.Status == PromptStatus.Error)
                psr = ed.SelectAll(filter);
            if (psr.Status != PromptStatus.OK)
                return;
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId id in psr.Value.GetObjectIds())
                    {
                        MText mt = (MText)tr.GetObject(id, OpenMode.ForWrite);
                        mt.Contents = GetStrippedMtextContents(mt);
                    }
                    tr.Commit();
                }
            }
            catch (System.Exception e)
            {
                ed.WriteMessage(string.Format("\nError: {0}\n", e.Message));
            }
        }

        [LispFunction("gc-GetStrippedMtextString")]
        public string GetStrippedMtextString(ResultBuffer resbuf)
        {
            try
            {
                if (resbuf == null)
                    throw new LispException("to few arguments");
                TypedValue[] arg = resbuf.AsArray();
                if (arg.Length > 1)
                    throw new LispException("to many arguments");
                if (arg[0].TypeCode != (short)LispDataType.ObjectId)
                    throw new LispException(string.Format("incorrect argument type: lentityp: {0}", arg[0].Value));
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    MText mt = tr.GetObject(((ObjectId)arg[0].Value), OpenMode.ForRead) as MText;
                    if (mt == null)
                        throw new LispException("incorrect argument type: not a mtext");
                    return GetStrippedMtextContents(mt);
                }
            }
            catch (LispException e)
            {
                AcAp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nError: {0}\n", e.Message);
                return null;
            }
        }
    }

    class LispException : System.Exception
    {
        public LispException(string message)
            : base(message)
        {
        }
    }
}

target audience:{intermediate}

84
Layouts and printing / Re: Check Paper/Model Space
« on: November 19, 2010, 07:08:18 PM »
Talking about short code, I forgot to post a F# example
Code: [Select]
let F_TestPaperSpace() =
    LayoutManager.Current.CurrentLayout <> "Model"

85
Layouts and printing / Re: Check Paper/Model Space
« on: November 19, 2010, 04:44:27 PM »
Hi,

As the function returns a Boolean, IMO there's no need for an If ... Then ... Else statement.
Don't make VB more verbose than it is  :clinoeil:

C#
Code: [Select]
private bool F_TestPaperSpace()
{
    return LayoutManager.Current.CurrentLayout != "Model";
}

VB
Code: [Select]
Private Function F_TestPaperSpace() As Boolean
Return LayoutManager.Current.CurrentLayout <> "Model"
End Function

86
Math and Geometry / Geometry extensions
« on: November 19, 2010, 04:01:42 PM »
Hi,

I already posted this at TheSwamp.
It will be my contribution to give a start to this new website.

These are some geometry extensions methods for Point2d, Point3d, CircularArc2d, Polyline and Polyline2d classes and two little classes: Triangle2d, Triangle3d.
I hope someones find them usefull and expect it shows a little basic OOP concepts as the creation of new types and the extension of already existing ones.

All these classes work together and have to be compiled in a single DLL.

The extension methods requires to reference System.Core (.NET Framework 3.5) and are available for these objects as native methods while GeometryExtensions.dll is referenced:
Code: [Select]
using GeometryExtensions;

Point3d p1 = new Point3d(10.0, 20.0, 0.0);
Point3d p2 = p1.Polar(Math.PI / 4.0, 10.0);
p2 coordinates are: (17.0710678118655,27.0710678118655,0)

The GeomExt class, contains the following extension methods:

Matrix3d Editor.UCS2WCS()
Matrix3d Editor.WCS2UCS()
Matrix3d Editor.DCS2WCS()
Matrix3d Editor.WCS2DCS()
Matrix3d Editor.DCS2PSDCS()
Matrix3d Editor.PSDCS2DCS()

Matrix3d Viewport.DCS2WCS()
Matrix3d Viewport.WCS2DCS()
Matrix3d Viewport.PSDCS2DCS()
Matrix3d Viewport.DCS2PSDCS()

Point3d Point2d.Convert3d()
Point3d Point2d.Convert3d(Plane plane)
Point3d Point2d.Convert3d(Vector3d normal, double elevation)
Point2d Point2d.Flatten(Vector3d normal)
bool IsBetween(Point2d p1, Point2d p2)
bool IsBetween(Point2d p1, Point2d p2, Tolerance tol)
Point2d Point2d.Polar(double angle, double distance)

Point3d Point3d.Convert2d()
Point3d Point3d.Flatten()
bool IsBetween(Point3d p1, Point3d p2)
bool IsBetween(Point3d p1, Point3d p2, Tolerance tol)
Point3d Point3d.Polar(double angle, double distance)
Point3d Point3d.Trans(CoordSystem from, CoordSystem to)
Point3d Point3d.Trans(Editor ed, CoordSystem from, CoordSystem to)

Vector3d Vector3d .Flatten()

void Point2dCollection.RemoveDuplicate()
void Point2dCollection.RemoveDuplicate(Tolerance tol)
bool Point2dCollection.Contains(Point2d pt, Tolerance tol)

void Point3dCollection.RemoveDuplicate()
void Point3dCollection.RemoveDuplicate(Tolerance tol)
bool Point3dCollection.Contains(Point3d pt, Tolerance tol)

double CircularArc2d.AlgebricArea()
Point2d CircularArc2d.Centroid()

Point2d Polyline.Centroid2d()
Point3d Polyline.Centroid()
Polyline Polyline.GetProjectedPolyline(Plane plane, Vector3d direction)
Polyline Polyline.GetOrthoProjectedPolyline(Plane plane)

Point3d Polyline2d.Centroid()
CircularArc3d Polyline2d.GetArcSegmentAt(int index)
CircularArc2d Polyline2d.GetArcSegment2dAt(int index)
LineSegment3d Polyline2d.GetLineSegmentAt(int index)
LineSegment2d Polyline2d.GetLineSegment2dAt(int index)
Polyline Polyline2d.GetProjectedPolyline(Plane plane, Vector3d direction)
Polyline Polyline2d.GetOrthoProjectedPolyline(Plane plane)
List<Vertex2d> Polyline2d.GetVertices()

Polyline Polyline3d.GetProjectedPolyline(Plane plane, Vector3d direction)
Polyline Polyline3d.GetOrthoProjectedPolyline(Plane plane)

Point3d Region.Centroid()

Point3d Spline.Centroid()

Polyline Ellipse.ToPolyline()


Triangle<T> abstract class

Constructors
Triangle()
Triangle(T[] pts)
Triangle(T a, T b, T c)

Indexor
T Item

Methods
T Inverse()
void Set(T[] pts)
void Set(T a, T b, T c)
T[] ToArray()


Triangle2d : Triangle<Point2d> class

Constructors
Triangle2d() : base()
Triangle2d(Point2d[] pts) : base(pts)
Triangle2d(Point2d a, Point2d b, Point2d c) : base(a, b, c)
Triangle2d(Point2d org, Vector2d v1, Vector2d v2)

Properties
double AlgebricArea
Point2d Centroid
CircularArc2d CircumscribedCircle
CircularArc2d InscribedCircle
bool IsClockwise

Methods
Triangle3d Convert3d(Plane plane)
Triangle3d Convert3d(Vector3d normal, double elevation)
double GetAngleAt(int index)
LineSegment2d GetSegmentAt(int index)
List<Point2d> IntersectWith(LinearEntity2d le2d)
List<Point2d> IntersectWith(LinearEntity2d le2d, Tolerance tol)
bool IsEqualTo(Triangle2d t2d)
bool IsEqualTo(Triangle2d t2d, Tolerance tol)
bool IsPointInside(Point2d pt)
bool IsPointOn(Point2d pt)
void Set(Point2d org, Vector2d v1, Vector2d v2)
Triangle2d TransformBy(Matrix2d mat)


Triangle3d : Triangle<Point3d> class

Constructors
Triangle3d() : base()
Triangle3d(Point3d[] pts) : base(pts)
Triangle3d(Point3d a, Point3d b, Point3d c) : base(a, b, c)
Triangle3d(Point3d org, Vector3d v1, Vector3d v2)

Properties
double Area
Point3d Centroid
CircularArc3d CircumscribedCircle
double Elevation
Vector3d GreatestSlope
Vector3d Horizontal
CircularArc3d InscribedCircle
bool IsHorizontal
Vector3d Normal
double SlopePerCent
Matrix3d SlopeUCS

Methods
Triangle2d Convert2d()
Triangle2d Flatten()
double GetAngleAt(int index)
BoundedPlane GetBoundedPlane()
Plane GetPlane()
LineSegment3d GetSegmentAt(int index)
bool IsEqualTo(Triangle3d t3d)
bool IsEqualTo(Triangle3d t3d, Tolerance tol)
bool IsPointInside(Point3d pt)
bool IsPointOn(Point3d pt)
void Set(Point3d org, Vector3d v1, Vector3d v2)
Triangle3d Transformby(Matrix3d mat)


PolylineSegment class

Constructors

PolylineSegment(Point2d startPoint, Point2d endPoint)
PolylineSegment(Point2d startPoint, Point2d endPoint, double bulge)
PolylineSegment(Point2d startPoint, Point2d endPoint, double bulge, double constantWidth)
PolylineSegment(Point2d startPoint, Point2d endPoint, double bulge, double startWidth, double endWidth)
PolylineSegment(Line2dSegment line)
PolylineSegment(CircularArc2d arc)

Properties

double Bulge
Point2d EndPoint
double EndWidth
bool IsLinear
Point2d StartPoint
double StartWidth

Methods

bool Equals(object obj)
int GetHashCode()
double GetParameterOf(Point3d pt)
void Inverse()
CircularArc2d ToCircularArc()
Curve2d ToCurve2d()
LineSegment2d ToLineSegment()
string ToString()


PolylineSegmentCollection : IList<PolylineSegment> class

Constructors

PolylineSegmentCollection()
PolylineSegmentCollection(Circle circle)
PolylineSegmentCollection(Ellipse ellipse)
PolylineSegmentCollection(Polyline pline)
PolylineSegmentCollection(Polyline2d pline)
PolylineSegmentCollection(IEnumerable<PolylineSegment> segments)

Methods

void Add(PolylineSegment segment)
void AddRange(IEnumerable<PolylineSegment> range)
void Clear()
bool Contains(PolylineSegment segment)
int FinIindex(Predicate<PolylineSegment> match)
int GetClosestSegmentTo(Point2d pt)
IEnumerator<PolylineSegment> GetEnumerator()
int IndexOf(PolylineSegment item)
void Insert(int index, PolylineSegment item)
void InsertRange(int index, IEnumerable<PolylineSegment> collection)
List<PolylineSegmentCollection> Join()
List<PolylineSegmentCollection> Join(Tolerance tol)
bool Remove(PolylineSegment item)
void RemoveAt(int index)
public void RemoveRange(int index, int count)
Polyline ToPolyline()

For more information, see the updated documentation.

GeometryExtensions(1.5).zip contains the C# source code files and the GeometryExtensions_18.dll (AutoCAD 2010-2012) and GeometryExtensions_19.dll (AutoCAD 2013)

<EDIT: version 1.5>


target audience:{intermediate}

Pages: 1 ... 4 5 [6]