Recent Posts

Pages: 1 ... 6 7 [8] 9 10
71
Selection sets / Re: Selection Ordinate Dimensions with Filter (dxf code)
« Last post by madmat on February 03, 2013, 12:13:07 PM »
Thanx Gile for your answers, I've tried your last solution and it's working fine.
72
Selection sets / Re: Selection Ordinate Dimensions with Filter (dxf code)
« Last post by (gile) on February 02, 2013, 03:36:48 PM »
The upper method isn't very strict, even it will work fine with ordinate dimensions because it uses a bitwise test on values wich aren't binary codes: it will work with 6, 5 (3 points angular dimensions), 3 (diameter dimension) but not with 4, 2, 1 or 0.

Another way, better in my opinion, is to combinate a classical selection filter and a Editor.SelectionAdded event handler to filter the selected objects.

C#
Code: [Select]
        [CommandMethod("Test")]
        public void test()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            TypedValue[] filter = { new TypedValue(0, "dimension"), new TypedValue(3, "ISO-25") };

            ed.SelectionAdded += ed_SelectionAdded;
            PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
            ed.SelectionAdded -= ed_SelectionAdded;
            if (psr.Status != PromptStatus.OK) return;
            ed.SetImpliedSelection(psr.Value);
        }

        void ed_SelectionAdded(object sender, SelectionAddedEventArgs e)
        {
            for (int i = 0; i < e.AddedObjects.Count; i++)
            {
                if (e.AddedObjects[i].ObjectId.ObjectClass.Name != "AcDbOrdinateDimension")
                    e.Remove(i);
            }
        }

VB
Code: [Select]
        <CommandMethod("Test")> _
        Public Sub test()
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

            Dim filter As TypedValue() = { New TypedValue(0, "dimension"), new TypedValue(3, "ISO-25") }

            AddHandler ed.SelectionAdded, AddressOf ed_SelectionAdded
            Dim psr As PromptSelectionResult = ed.GetSelection(New SelectionFilter(filter))
            RemoveHandler ed.SelectionAdded, AddressOf ed_SelectionAdded
            If psr.Status <> PromptStatus.OK Then
                Return
            End If
            ed.SetImpliedSelection(psr.Value)
        End Sub

        Private Sub ed_SelectionAdded(sender As Object, e As SelectionAddedEventArgs)
            For i As Integer = 0 To e.AddedObjects.Count - 1
                If e.AddedObjects(i).ObjectId.ObjectClass.Name <> "AcDbOrdinateDimension" Then
                    e.Remove(i)
                End If
            Next
        End Sub
73
Selection sets / Re: Selection Ordinate Dimensions with Filter (dxf code)
« Last post by (gile) on February 02, 2013, 02:27:08 PM »
Hi,

Look at the 70 dxf group.

Code: [Select]
TypedValue[] tvs = {
    new TypedValue(0, "dimension"),
    new TypedValue(3, "ISO-25"),
    new TypedValue(-4, "&="),
    new TypedValue(70, 6)};

SelectionFilter filter = new SelectioFilter(tvs);

PromptSelectionResult psr = ed.GetSelection(filter);
74
Selection sets / Selection Ordinate Dimensions with Filter (dxf code)
« Last post by madmat on February 01, 2013, 11:15:43 PM »
Hello,

I need your help... (my problem is on the 3rd "MyParam.Setvalue" line)
I try to make a SelectionSet with Filter, in order to have only Ordinate Dimensions in my SelectionSet.
I've done following, but the "100" dxfCode doesn't seem to work (but the 2 other do).

-----------------
Dim MyParam(2) As TypedValue
MyParam.SetValue(New TypedValue(0, "Dimension"), 0)
MyParam.SetValue(New TypedValue(DxfCode.DimStyleName, "ISO-25"), 1)
MyParam.SetValue(New TypedValue(100, "AcDbOrdinateDimension"), 2)  '<-This one doesn't seem to work...

Dim MyFilter As SelectionFilter = New SelectionFilter(MyParam)

Dim PSR As PromptSelectionResult = myEd.GetSelection(MyFilter)
-----------------


I've tried to write "DxfCode.Subclass" instead of "100" but the result was the same.
When I type "(entget(car(entsel)))" in Autocad, I find "AcDbOrdinateDimension" for the 100 dxfCode.
Does anybody know what to do?
Is there maybe another way to filter this selection?
75
This site / Re: Welcome on the AutoCAD .NET developpers forums
« Last post by bcinnv on February 01, 2013, 04:42:21 AM »
Hello!
76
Blocks / Drag (attributed) block reference
« Last post by (gile) on January 26, 2013, 10:44:15 PM »
Dragging and rotating an (attributed) block.

BlockJig is to be used to drag (and possibly rotate) a simple block.
While dragging the block, it's possible to dynamically rotate the block by pressing Ctrl.
A click terminate the process keeping the current position and rotation for the block

BlockAttribJig inherits from BlockJig and handles the block attribute references.
In the constructor, each attribute definition informations (position, alignement and rotation) is stored in a structure.
The overrided Update() method sets the attributes geometry according to the attribute definition informations and the block reference position and rotation.

Code: (csharp) [Select]
        class BlockJig : EntityJig
        {
            protected BlockReference _br;
            protected Point3d _pos;
            protected double _rot, _ucsRot;

            public BlockJig(BlockReference br)
                : base(br)
            {
                _br = br;
                _pos = _br.Position;
                Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
                CoordinateSystem3d ucs = ed.CurrentUserCoordinateSystem.CoordinateSystem3d;
                Matrix3d ocsMat = Matrix3d.WorldToPlane(new Plane(Point3d.Origin, ucs.Zaxis));
                _ucsRot = Vector3d.XAxis.GetAngleTo(ucs.Xaxis.TransformBy(ocsMat), ucs.Zaxis);
                _rot = _br.Rotation - _ucsRot;
            }

            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                System.Windows.Forms.Keys mods = System.Windows.Forms.Control.ModifierKeys;
                if ((mods & System.Windows.Forms.Keys.Control) > 0)
                {
                    JigPromptAngleOptions jpao =
                        new JigPromptAngleOptions("\nSpecify the rotation: ");
                    jpao.UseBasePoint = true;
                    jpao.BasePoint = _br.Position;
                    jpao.Cursor = CursorType.RubberBand;
                    jpao.UserInputControls = (
                        UserInputControls.Accept3dCoordinates |
                        UserInputControls.UseBasePointElevation);
                    PromptDoubleResult pdr = prompts.AcquireAngle(jpao);

                    if (_rot == pdr.Value)
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        _rot = pdr.Value;
                        return SamplerStatus.OK;
                    }
                }
                else
                {
                    JigPromptPointOptions jppo =
                        new JigPromptPointOptions("\nSpecify insertion point (or press Ctrl for rotation): ");
                    jppo.UserInputControls =
                      (UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted);
                    PromptPointResult ppr = prompts.AcquirePoint(jppo);
                    if (_pos.DistanceTo(ppr.Value) < Tolerance.Global.EqualPoint)
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        _pos = ppr.Value;
                    }
                    return SamplerStatus.OK;
                }
            }

            protected override bool Update()
            {
                _br.Position = _pos;
                _br.Rotation = _rot +_ucsRot;
                return true;
            }
        }

        class BlockAttribJig : BlockJig
        {
            struct TextInfo
            {
                public Point3d Position;
                public Point3d Alignment;
                public double Rotation;
                public bool IsAligned;
            }

            private Dictionary<string, TextInfo> _attInfos;

            public BlockAttribJig(BlockReference br)
                : base(br)
            {
                _attInfos = new Dictionary<string, TextInfo>();
                BlockTableRecord btr = (BlockTableRecord)br.BlockTableRecord.GetObject(OpenMode.ForRead);
                foreach (ObjectId id in btr)
                {
                    if (id.ObjectClass.Name == "AcDbAttributeDefinition")
                    {
                        AttributeDefinition attDef = (AttributeDefinition)id.GetObject(OpenMode.ForRead);
                        TextInfo ti = new TextInfo()
                        {
                            Position = attDef.Position,
                            Alignment = attDef.AlignmentPoint,
                            IsAligned = attDef.Justify != AttachmentPoint.BaseLeft,
                            Rotation = attDef.Rotation
                        };
                        _attInfos.Add(attDef.Tag.ToUpper(), ti);
                    }
                }
            }

            protected override bool Update()
            {
                base.Update();
                foreach (ObjectId id in _br.AttributeCollection)
                {
                    AttributeReference att = (AttributeReference)id.GetObject(OpenMode.ForWrite);
                    att.Rotation = _br.Rotation;
                    string tag = att.Tag.ToUpper();
                    if (_attInfos.ContainsKey(tag))
                    {
                        TextInfo ti = _attInfos[tag];
                        att.Position = ti.Position.TransformBy(_br.BlockTransform);
                        if (ti.IsAligned)
                        {
                            att.AlignmentPoint =
                                ti.Alignment.TransformBy(_br.BlockTransform);
                            att.AdjustAlignment(_br.Database);
                        }
                        if (att.IsMTextAttribute)
                        {
                            att.UpdateMTextAttribute();
                        }
                        att.Rotation = ti.Rotation + _br.Rotation;
                    }
                }
                return true;
            }
        }

A little using example to insert a block

Code: (csharp) [Select]
        [CommandMethod("TEST")]
        public void Test()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptResult pr = ed.GetString("\nBlock name: ");
            if (pr.Status != PromptStatus.OK) return;
            string blockName = pr.StringResult;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (!bt.Has(blockName))
                {
                    ed.WriteMessage("\nNone block '{0}' in the document block table.", blockName);
                    return;
                }
                BlockTableRecord curSpace =
                    (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                // Add the block reference to Database first
                BlockReference br = new BlockReference(Point3d.Origin, bt[blockName]);
                br.TransformBy(ed.CurrentUserCoordinateSystem);
                curSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);

                // Get the block definition
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(bt[blockName], OpenMode.ForRead);
                BlockJig jig;
                if (btr.HasAttributeDefinitions)
                {
                    // Add attribute references to the block reference
                    foreach (ObjectId id in btr)
                    {
                        if (id.ObjectClass.Name == "AcDbAttributeDefinition")
                        {
                            AttributeDefinition attDef =
                                (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
                            AttributeReference attRef = new AttributeReference();
                            attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
                            ObjectId attId = br.AttributeCollection.AppendAttribute(attRef);
                            tr.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }
                    // Create a BlockAttribJig instance
                    jig = new BlockAttribJig(br);
                }
                else
                {
                    // Create a BlockJig instance
                    jig = new BlockJig(br);
                }
                // Drag the block reference
                pr = ed.Drag(jig);
                if (pr.Status != PromptStatus.OK) br.Erase();
                tr.Commit();
            }
        }
77
Text / place Mtext to middle between two curves
« Last post by fixo on January 25, 2013, 09:03:36 PM »
This code allow to add many lines in the command line one by another in loop
To start command type : mit
 
Code: [Select]
      [CommandMethod("PlaceMtextToMidpoint","mit", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public void PlaceMtextToMiddle()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect the first curve: ");
            peo.SetRejectMessage("Only a curve !");
            peo.AddAllowedClass(typeof(Curve), false);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            ObjectId id1 = per.ObjectId;
            Point3d p1 = per.PickedPoint;
            peo.Message = "\nSelect the second curve: ";
            per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            ObjectId id2 = per.ObjectId;

            PromptStringOptions pso = new PromptStringOptions("\nEnter first line of Mtext: ");
            pso.AllowSpaces = true;
            PromptResult res;
            res = ed.GetString(pso);
            if (res.Status != PromptStatus.OK)
                return;

            string txtStr = res.StringResult;
            do
            {
                pso.Message = "\nEnter next line of Mtext: ";
                res = ed.GetString(pso);
                if (res.Status != PromptStatus.OK)
                    return;
                txtStr = txtStr + "\\P" + res.StringResult;
            } while (res.Status != PromptStatus.OK);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                DBObject obj1 = (DBObject)tr.GetObject(id1, OpenMode.ForRead, false);
                Curve curve1 = obj1 as Curve;

                if (curve1 == null)
                {
                    ed.WriteMessage("\nNo curve selected\n");
                    return;
                }
                DBObject obj2 = (DBObject)tr.GetObject(id2, OpenMode.ForRead, false);
                Curve curve2 = obj2 as Curve;

                if (curve2 == null)
                {
                    ed.WriteMessage("\nNo curve selected\n");
                    return;
                }
                Point3d p2 = curve2.GetClosestPointTo(p1, false);

                Plane plan = curve1.GetPlane();

                Vector3d norm = plan.Normal;

                p1 = p1.TransformBy(Matrix3d.WorldToPlane(norm));

                p2 = p2.TransformBy(Matrix3d.WorldToPlane(norm));
                // calculate middle point one way
                Point3d mp = p1.Add(p2 - p1).ScaleBy(0.5, p1).TransformBy(Matrix3d.WorldToPlane(norm));

                double ang = (p2 - p1).AngleOnPlane(plan);
                // draw vectors for imagination
                ed.DrawVector(p1, mp, 1, true);

                ed.DrawVector(p2, mp, 5, true);
                // set angle to be more readable
                if ((ang > Math.PI / 2) && (ang < Math.PI * 1.5))
                    ang = ang + Math.PI;
                // create mtext
                MText mtxt = new MText();
                mtxt.SetDatabaseDefaults();
                mtxt.Attachment = AttachmentPoint.MiddleCenter;
                mtxt.Location = mp;
                mtxt.TextHeight = db.Textsize;
                mtxt.Width = 0.0;
                mtxt.Contents = txtStr;
                mtxt.Layer = "0";
                mtxt.ColorIndex = 256;
                Matrix3d mx = Matrix3d.Rotation(ang, norm, mp);
                mtxt.TransformBy(mx);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                // add mtext to database and transaction
                btr.AppendEntity(mtxt);
                tr.AddNewlyCreatedDBObject(mtxt, true);

                tr.Commit();
            }
        }
78
Blocks / Re: How to edit block parameters from a windows form (c#)?
« Last post by fixo on January 22, 2013, 08:32:28 PM »
Sorry for the belating JohannesRP,
You can use something like this, tested on my A2010 with good result
Code: [Select]
                    if (b.Name == "WT")
                        {
// HELP!!!
// Here i need to change the properties of the block with name "WT"
                            foreach (object item in b)
                            {
                                Autodesk.AutoCAD.Interop.Common.AcadLine ln = item as Autodesk.AutoCAD.Interop.Common.AcadLine;
                                if (ln != null)
                                {
                                    ln.Linetype = "Center";
                                    ln.LinetypeScale = 0.01;
                                    Autodesk.AutoCAD.Interop.Common.AcadAcCmColor acCol = ln.TrueColor;
                                    acCol.ColorIndex = Autodesk.AutoCAD.Interop.Common.AcColor.acCyan;
                                    ln.TrueColor = acCol;
                                }
                            }                         
                        }
79
Math and Geometry / Re: Geometry extensions
« Last post by (gile) on January 21, 2013, 08:13:21 PM »
I updated the attachment and documentation in the first message.
80
Visual Studio environment / Re: Search window doesn't stay put
« Last post by samenatina1 on January 19, 2013, 02:15:11 PM »
This is a great post. Thanks for sharing this
Pages: 1 ... 6 7 [8] 9 10