Message 1 of 1
dimension extension hideC#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
HI I am trying to On and OFF the dimension extension with nearest point selection I tried elow code ut its not suppressing the extension
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DimLine
{
public class DimensionExtensionLineToggle
{
[CommandMethod("ToggleDimExtensionLine")]
public void ToggleDimExtensionLine()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
// Prompt for entity selection, not filtering here
PromptEntityOptions opt = new PromptEntityOptions("\nSelect a dimension: ");
PromptEntityResult res = ed.GetEntity(opt);
if (res.Status != PromptStatus.OK)
return;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
Entity entity = tr.GetObject(res.ObjectId, OpenMode.ForWrite) as Entity;
// Ensure the selected entity is a dimension
if (entity is Dimension dim)
{
// Prompt for a point selection near the dimension
PromptPointResult pointRes = ed.GetPoint("\nSelect a point near the dimension: ");
if (pointRes.Status != PromptStatus.OK)
return;
Point3d selectedPoint = pointRes.Value;
// Get extension line points based on dimension type
Point3d? extLine1Start, extLine2Start;
GetDimensionExtensionLinePoints(dim, out extLine1Start, out extLine2Start);
if (extLine1Start.HasValue && extLine2Start.HasValue)
{
bool isNear = IsNearDimensionExtensionLine(selectedPoint, extLine1Start.Value, extLine2Start.Value);
// Toggle visibility based on proximity
ToggleExtensionLines(dim, isNear);
tr.Commit();
}
}
else
{
ed.WriteMessage("\nThe selected entity is not a valid dimension.");
}
}
}
private void GetDimensionExtensionLinePoints(Dimension dim, out Point3d? extLine1Start, out Point3d? extLine2Start)
{
extLine1Start = null;
extLine2Start = null;
if (dim is RotatedDimension rotatedDim)
{
extLine1Start = rotatedDim.XLine1Point;
extLine2Start = rotatedDim.XLine2Point;
}
else if (dim is AlignedDimension alignedDim)
{
extLine1Start = alignedDim.XLine1Point;
extLine2Start = alignedDim.XLine2Point;
}
else if (dim is RadialDimension radialDim)
{
extLine1Start = radialDim.Center;
extLine2Start = radialDim.ChordPoint;
}
}
private bool IsNearDimensionExtensionLine(Point3d selectedPoint, Point3d extLine1Start, Point3d extLine2Start)
{
double thresholdDistance = 1.0;
double distanceToExtLine1 = selectedPoint.DistanceTo(extLine1Start);
double distanceToExtLine2 = selectedPoint.DistanceTo(extLine2Start);
return distanceToExtLine1 < thresholdDistance || distanceToExtLine2 < thresholdDistance;
}
private void ToggleExtensionLines(Dimension dim, bool isNear)
{
string appName = "ExtensionLineToggleApp";
ObjectId appId = EnsureAppIdRegistered(appName);
using (ResultBuffer rb = new ResultBuffer(
new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName),
new TypedValue((int)DxfCode.ExtendedDataInteger32, isNear ? 1 : 0)
))
{
dim.XData = rb;
}
if (isNear)
{
dim.Dimexe = 0.0;
}
else
{
dim.Dimexe = 1.0;
}
}
private ObjectId EnsureAppIdRegistered(string appName)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId appId = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
RegAppTable regAppTbl = tr.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable;
if (!regAppTbl.Has(appName))
{
regAppTbl.UpgradeOpen();
RegAppTableRecord regAppTblRec = new RegAppTableRecord
{
Name = appName
};
appId = regAppTbl.Add(regAppTblRec);
tr.AddNewlyCreatedDBObject(regAppTblRec, true);
}
else
{
appId = regAppTbl[appName];
}
tr.Commit();
}
return appId;
}
}
}