• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Valued Contributor
    Posts: 70
    Registered: ‎08-20-2012
    Accepted Solution

    How can I use a graphic ellipse with text together?

    185 Views, 18 Replies
    03-15-2013 08:24 PM

    Hello, everyone

    I want to create a new order. when  using the order,It will create a  ellipse and  a DTEXT .I mean the ellipse and the DTEXT are a whole. when move the ellipse the DTEXT will alse move.However, users can  edit the  DTEXT.

    Someone who can give  me some solution? Thanks very much!

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,338
    Registered: ‎10-08-2008

    Re: How can I use a graphic ellipse with text together?

    03-16-2013 07:49 AM in reply to: kevinsir

    Hi Kevin,

    Do you want to create the block with one attribute inside the ellipse?

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,338
    Registered: ‎10-08-2008

    Re: How can I use a graphic ellipse with text together?

    03-16-2013 08:39 AM in reply to: kevinsir

    Though you could be read same code in Labs,

    here is quick example

        [CommandMethod("OrderBlock", CommandFlags.Modal | CommandFlags.Redraw)]
            public static void CreateOrderLabel()
            {
                // Get the current document and database
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    
                Database db = doc.Database;
    
                Editor ed = doc.Editor;
    
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
    
                        // get current text size for attribute or use hard coded one instead
                        double txtheight = db.Textsize;
                        // block name
                        string blkName = "ORDER";
                        // check if exists
                        if (bt.Has(blkName))
                        {
                            ed.WriteMessage("\nBlock \"ORDER\" already exist.");
                            return;// if exists then exit
                        }
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        Point3d inspt = new Point3d(0, 0, 0);
                        BlockTableRecord newBtr = new BlockTableRecord();
                        bt.Add(newBtr);
    
                        // change settings for you block to your suit:
                        newBtr.Name = blkName;
                        newBtr.Origin = inspt;
                        newBtr.BlockScaling = BlockScaling.Uniform;
                        newBtr.Units = UnitsValue.Inches;
                        newBtr.Explodable = false;
                        tr.AddNewlyCreatedDBObject(newBtr, true);
    
                        Vector3d normal = Vector3d.ZAxis;
                        Vector3d majorAxis = 6 * txtheight * Vector3d.XAxis;// change major axis to your suit
                        double radiusRatio = 0.4;// change ratio to your suit
                        double startAng = 0.0;
                        double endAng = Math.PI * 2;
                        Ellipse ellipse = new Ellipse(inspt, normal, majorAxis, radiusRatio, startAng, endAng);
                        ellipse.Layer = "0";
                        ellipse.LinetypeId = db.ContinuousLinetype;
                        ellipse.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0);// color by block
                        ellipse.LineWeight = LineWeight.LineWeight040;// wide lineweight 0.40, change on LineWeight.ByLineWeightDefault to discard it
                        newBtr.AppendEntity(ellipse);
                        tr.AddNewlyCreatedDBObject(ellipse, true);
    
                        AttributeDefinition attr = new AttributeDefinition();
                        attr.Layer = "0";
                        attr.LinetypeId = db.ContinuousLinetype;
                        attr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0);// color by block
                        attr.LineWeight = LineWeight.ByLineWeightDefault;
                        attr.Tag = "DESCRIPTION";
                        attr.Prompt = "Order description:";
                        attr.TextString = "blah";
                        attr.Preset = true;
    
                        //attr.TextStyle = db.Textstyle;//<--   A2009
                        attr.TextStyleId = db.Textstyle;//<--   A2010
                        attr.Height = txtheight;
                        attr.Position = inspt;
                        attr.Justify = AttachmentPoint.MiddleCenter;
                        attr.AlignmentPoint = inspt;
                        attr.LockPositionInBlock = true;
                        attr.AdjustAlignment(db);
                        newBtr.AppendEntity(attr);
                        tr.AddNewlyCreatedDBObject(attr, true);
    
                        tr.Commit();
                        ed.Regen();
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
                    }
                }
            }

     

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Valued Contributor
    Posts: 70
    Registered: ‎08-20-2012

    Re: How can I use a graphic ellipse with text together?

    03-16-2013 08:57 AM in reply to: kevinsir

    Thanks for your repy.If the attribute can show  and edit on the paper , It is maybe perfect.

    I will try your code.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,338
    Registered: ‎10-08-2008

    Re: How can I use a graphic ellipse with text together?

    03-16-2013 09:54 AM in reply to: kevinsir

    Here is VB.NET code,see my poor comments inline

            <CommandMethod("OrderBlock", CommandFlags.Modal Or CommandFlags.Redraw)> _
            Public Shared Sub CreateOrderLabel()
                ' Get the current document and database
                Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    
                Dim db As Database = doc.Database
    
                Dim ed As Editor = doc.Editor
    
                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    Try
                        Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
    
                        ' get current text size for attribute or use hard coded one instead
                        Dim txtheight As Double = db.Textsize
                        ' block name
                        Dim blkName As String = "ORDER"
                        ' check if exists
                        If bt.Has(blkName) Then
                            ed.WriteMessage(vbLf & "Block ""ORDER"" already exist.")
                            ' if exists then exit
                            Return
                        End If
                        Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                        Dim inspt As New Point3d(0, 0, 0)
                        Dim newBtr As New BlockTableRecord()
                        bt.Add(newBtr)
    
                        ' change settings for you block to your suit:
                        newBtr.Name = blkName
                        newBtr.Origin = inspt
                        newBtr.BlockScaling = BlockScaling.Uniform
                        newBtr.Units = UnitsValue.Inches
                        newBtr.Explodable = False
                        tr.AddNewlyCreatedDBObject(newBtr, True)
    
                        Dim normal As Vector3d = Vector3d.ZAxis
                        Dim majorAxis As Vector3d = 6 * txtheight * Vector3d.XAxis
                        ' change major axis to your suit
                        Dim radiusRatio As Double = 0.4
                        ' change ratio to your suit
                        Dim startAng As Double = 0.0
                        Dim endAng As Double = Math.PI * 2
                        Dim ellipse As New Ellipse(inspt, normal, majorAxis, radiusRatio, startAng, endAng)
                        ellipse.Layer = "0"
                        ellipse.LinetypeId = db.ContinuousLinetype
                        ellipse.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0)
                        ' color by block
                        ellipse.LineWeight = LineWeight.LineWeight040
                        ' wide lineweight 0.40, change on LineWeight.ByLineWeightDefault to discard it
                        newBtr.AppendEntity(ellipse)
                        tr.AddNewlyCreatedDBObject(ellipse, True)
    
                        Dim attr As New AttributeDefinition()
                        attr.Layer = "0"
                        attr.LinetypeId = db.ContinuousLinetype
                        attr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0)
                        ' color by block
                        attr.LineWeight = LineWeight.ByLineWeightDefault
                        attr.Tag = "DESCRIPTION"
                        attr.Prompt = "Order description:"
                        attr.TextString = "blah"
                        attr.Preset = True
                        ' using current textstyle
                        'attr.TextStyle = db.Textstyle ''<--   A2009
                        attr.TextStyleId = db.Textstyle ''<--   A2010
                        attr.Height = txtheight
                        attr.Position = inspt
                        attr.Justify = AttachmentPoint.MiddleCenter
                        attr.AlignmentPoint = inspt
                        attr.LockPositionInBlock = True
                        attr.AdjustAlignment(db)
                        newBtr.AppendEntity(attr)
                        tr.AddNewlyCreatedDBObject(attr, True)
    
                        tr.Commit()
                        ed.Regen()
                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        ed.WriteMessage(ex.Message + vbLf + ex.StackTrace)
                    End Try
                End Using
            End Sub

     

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Valued Contributor
    Posts: 70
    Registered: ‎08-20-2012

    Re: How can I use a graphic ellipse with text together?

    03-16-2013 08:48 PM in reply to: Hallex

    Thanks a lot ! It is a nice solution. However, is there any better idea that if i want to edit description on the screen directly,never show a description dialogue.Because my customers use CAD on the pad ,maybe they want a big input UI.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,338
    Registered: ‎10-08-2008

    Re: How can I use a graphic ellipse with text together?

    03-16-2013 11:25 PM in reply to: kevinsir

    In this case discard show dialog using ATTDIA variable, see Help file

    Cheers :smileyhappy:

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Valued Contributor
    Posts: 70
    Registered: ‎08-20-2012

    Re: How can I use a graphic ellipse with text together?

    03-17-2013 12:19 AM in reply to: Hallex

    Can you tell me a more detailed method? Do  you mean that  when editing the attribute,  the dialogue also does not  appear ? 

    Please use plain text.
    Valued Contributor
    Posts: 70
    Registered: ‎08-20-2012

    Re: How can I use a graphic ellipse with text together?

    03-17-2013 04:25 AM in reply to: kevinsir

    I mean can I  use this attribute like dbtext,

    and another question is that is therer any setting i can set the attibute or the ellipse to be  visible or invisible!

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,338
    Registered: ‎10-08-2008

    Re: How can I use a graphic ellipse with text together?

    03-17-2013 06:03 AM in reply to: kevinsir

    I have not have a time for testing and could not reach at AutoCAD now,

    but you can play with attribite mode,

    say specify your settings instead, e.g. in this line of code:

    attr.Preset = True

    You may want to play with something like

    attr.Verify=true, same for Visible etc

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.