.NET

Reply
Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 1 of 3 (642 Views)
Accepted Solution

Adjust attribute text width

642 Views, 2 Replies
06-25-2013 12:32 AM

Hi All,

 

Is there any way to adjust width of attribute text so that it is not out of contained block. The attribute is set at left alignment and width factor <=1. Please see picture below:

 

test.png

 

Thanks so much.

You need to find out the attribte text's width, and then you need to find out the allowed/desired width. Once you get these 2 widths, you can calculate the WidthFactor of AttributeReference.

 

I put together some quick code that does it:

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(AttributeWidthFactor.MyCommands))]
namespace AttributeWidthFactor
{
public class MyCommands
{
[CommandMethod("AttWidth")]
public static void SetAttrWidthFactor()
{
Document dwg = Application.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
//Pick an attributereference in a block
PromptNestedEntityOptions opt = new
PromptNestedEntityOptions("\nPick an attribute:");
PromptNestedEntityResult res = ed.GetNestedEntity(opt);
if (res.Status == PromptStatus.OK)
{
if (res.ObjectId.ObjectClass.DxfName.ToUpper() == "ATTRIB")
{
//Ask user to pick a distance as desired width for
//the attribute to fit in. Based on the block, the width
//could be a known value
PromptPointOptions popt = new
PromptPointOptions("\nPick width base point:");
PromptPointResult pres = ed.GetPoint(popt);
if (pres.Status != PromptStatus.OK) return;
Point3d basePt = pres.Value;
PromptDistanceOptions dopt =
new PromptDistanceOptions("\nPick width: ");
dopt.UseBasePoint = true;
dopt.BasePoint = basePt;
PromptDoubleResult dres = ed.GetDistance(dopt);
if (dres.Status != PromptStatus.OK) return;
//This is the width we want to fit the attribute text's width
double w = dres.Value;
using (Transaction tran =
dwg.TransactionManager.StartTransaction())
{
AttributeReference att = (AttributeReference)tran.GetObject(
res.ObjectId, OpenMode.ForWrite);
//Get attribute's width, assuming it is placed horizontally
double aw = Math.Abs(att.GeometricExtents.MaxPoint.X
- att.GeometricExtents.MinPoint.X);
//This is the WidthFactor
double factor = w / aw;
att.WidthFactor = factor;
tran.Commit();
}
}
else
{
Application.ShowAlertDialog("Not an attribute!");
}
}
}
}
}

 The code will stretch or narrow the attribute to fit it into a given width.

 

HTH

*Expert Elite*
norman.yuan
Posts: 1,051
Registered: ‎04-27-2009
Message 2 of 3 (611 Views)

Re: Adjust attribute text width

06-25-2013 02:24 PM in reply to: xdbk07

You need to find out the attribte text's width, and then you need to find out the allowed/desired width. Once you get these 2 widths, you can calculate the WidthFactor of AttributeReference.

 

I put together some quick code that does it:

 

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(AttributeWidthFactor.MyCommands))]

namespace AttributeWidthFactor
{
    public class MyCommands
    {
        [CommandMethod("AttWidth")]
        public static void SetAttrWidthFactor()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            //Pick an attributereference in a block
            PromptNestedEntityOptions opt = new 
                PromptNestedEntityOptions("\nPick an attribute:");
            PromptNestedEntityResult res = ed.GetNestedEntity(opt);

            if (res.Status == PromptStatus.OK)
            {
                if (res.ObjectId.ObjectClass.DxfName.ToUpper() == "ATTRIB")
                {
                    //Ask user to pick a distance as desired width for 
                    //the attribute to fit in. Based on the block, the width 
                    //could be a known value
                    PromptPointOptions popt = new 
                        PromptPointOptions("\nPick width base point:");
                    PromptPointResult pres = ed.GetPoint(popt);
                    if (pres.Status != PromptStatus.OK) return;
                    Point3d basePt = pres.Value;

                    PromptDistanceOptions dopt = 
                        new PromptDistanceOptions("\nPick width: ");
                    dopt.UseBasePoint = true;
                    dopt.BasePoint = basePt;

                    PromptDoubleResult dres = ed.GetDistance(dopt);
                    if (dres.Status != PromptStatus.OK) return;

                    //This is the width we want to fit the attribute text's width
                    double w = dres.Value;

                    using (Transaction tran = 
                        dwg.TransactionManager.StartTransaction())
                    {
                        AttributeReference att = (AttributeReference)tran.GetObject(
                            res.ObjectId, OpenMode.ForWrite);

                        //Get attribute's width, assuming it is placed horizontally
                        double aw = Math.Abs(att.GeometricExtents.MaxPoint.X 
                            - att.GeometricExtents.MinPoint.X);

                        //This is the WidthFactor
                        double factor = w / aw;
                        att.WidthFactor = factor;

                        tran.Commit();
                    }
                }
                else
                {
                    Application.ShowAlertDialog("Not an attribute!");
                }
            }
        }
    }
}

 The code will stretch or narrow the attribute to fit it into a given width.

 

HTH

Active Contributor
xdbk07
Posts: 42
Registered: ‎11-06-2012
Message 3 of 3 (577 Views)

Re: Adjust attribute text width

06-26-2013 06:08 PM in reply to: xdbk07

Thanks a lot. It's OK.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!