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 of 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);
The Widthfactor
Double factor = W/aw;
Att. Widthfactor = factor;
Tran.commit ();
}
}
Else
{
Application.showalertdialog ("Not an attribute!");
}
}
}
}
}