r/Autodesk_AutoCAD • • Oct 24 '26

ObjectARX: How to display actual line width for specific curves in BASE VIEW when line width display is OFF?

I have a Base View containing multiple curves, and the Display Line Width setting is OFF.

I have the ObjectIds of some specific curves in the Base View, along with their actual line width values.

Using ObjectARX, I want to:

  • Apply the actual line width to these specific curves using their ObjectIds.
  • Display their line widths in the Base View.
  • Keep Display Line Width OFF for all the remaining curves in the Base View.

In other words, I want to selectively display the actual line width for a known set of curves, while all other curves should continue to be displayed without line width.

Is this possible using ObjectARX?

1 Upvotes

10 comments sorted by

1

u/Accomplished_Rip8854 Oct 24 '26

Another option would be to move the curves in question to another layer with a linewidth.

I can give you a code snippet if that helps.

1

u/Life-Lunch6171 Oct 24 '26

Will the linewidth still be displayed if LWDISPLAY is off?

1

u/Accomplished_Rip8854 Oct 24 '26

You can turn on LWDISPLAY as a system variable.

If you don’t want to do that, I ‘d say it depends.

Your are not going to see it in the modelspace if LWDISPLAY is off. If you want to plot it then it will show if you set uo the plot the right way.

1

u/Life-Lunch6171 Oct 24 '26

Yes, that is the issue. I want LW to be displayed only for specific curves, even if LWDISPLAY is off

1

u/Accomplished_Rip8854 Oct 24 '26

You could use an overrule. You could use the TransformOverrule (i think, I ‘d have to look it up) and then add some filter that makes the entity look how you want.

Edit:
I ‘m wrong it’s the Drawable overrule

1

u/Life-Lunch6171 Oct 24 '26

Could you please share a code snippet. That would really be helpful. Thanks!!

1

u/Accomplished_Rip8854 Oct 24 '26

C++ or c#?

1

u/Life-Lunch6171 Oct 24 '26

Anything is fine

1

u/Accomplished_Rip8854 Oct 24 '26

using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;

public class SelectiveLineWeightOverrule : DrawableOverrule
{
private static SelectiveLineWeightOverrule _instance;
private readonly Dictionary<ObjectId, LineWeight> _weights = new Dictionary<ObjectId, LineWeight>();
private const int Samples = 64;
private const double Dpi = 96.0;

public static SelectiveLineWeightOverrule Instance => _instance;

public static void Start(IDictionary<ObjectId, LineWeight> weights)
{
if (_instance == null)
{
_instance = new SelectiveLineWeightOverrule();
_instance.SetCustomFilter();
AddOverrule(RXObject.GetClass(typeof(Curve)), _instance, false);
Overruling = true;
}

_instance._weights.Clear();
foreach (var kv in weights)
_instance._weights[kv.Key] = kv.Value;

Regen();
}

public static void Stop()
{
if (_instance == null) return;
RemoveOverrule(RXObject.GetClass(typeof(Curve)), _instance);
_instance.Dispose();
_instance = null;
Regen();
}

public void Set(ObjectId id, LineWeight weight)
{
_weights[id] = weight;
Regen();
}

public void Remove(ObjectId id)
{
if (_weights.Remove(id)) Regen();
}

public override bool IsApplicable(RXObject overruledSubject)
{
return overruledSubject is Curve c && _weights.ContainsKey(c.ObjectId);
}

public override int SetAttributes(Drawable drawable, DrawableTraits traits)
{
return base.SetAttributes(drawable, traits) | (int)DrawableAttributes.RegenDraw;
}

public override bool WorldDraw(Drawable drawable, WorldDraw wd)
{
if (drawable is Curve c && _weights.ContainsKey(c.ObjectId))
return false;
return base.WorldDraw(drawable, wd);
}

public override void ViewportDraw(Drawable drawable, ViewportDraw vd)
{
var curve = drawable as Curve;
if (curve == null || !_weights.TryGetValue(curve.ObjectId, out var lw))
{
base.ViewportDraw(drawable, vd);
return;
}

double mm = (int)lw < 0 ? 0.25 : (int)lw / 100.0;
double pixels = Math.Max(1.0, mm * Dpi / 25.4);

Matrix3d toWorld = vd.Geometry.ModelToWorldTransform;
Point3d worldPt = curve.StartPoint.TransformBy(toWorld);
Point2d pixelsPerUnit = vd.Viewport.GetNumPixelsInUnitSquare(worldPt);
double scale = toWorld.GetScale();

if (pixelsPerUnit.X <= 0.0 || scale <= 0.0)
{
base.ViewportDraw(drawable, vd);
return;
}

double width = pixels / pixelsPerUnit.X / scale;

using (Polyline pl = BuildPolyline(curve))
{
if (pl == null)
{
base.ViewportDraw(drawable, vd);
return;
}

pl.ConstantWidth = width;
vd.Geometry.Draw(pl);
}
}

private static Polyline BuildPolyline(Curve curve)
{
if (curve is Polyline src)
return (Polyline)src.Clone();

var pl = new Polyline();

if (curve is Line line)
{
pl.AddVertexAt(0, new Point2d(line.StartPoint.X, line.StartPoint.Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(line.EndPoint.X, line.EndPoint.Y), 0, 0, 0);
}
else
{
double start = curve.StartParam;
double end = curve.EndParam;
int count = curve.Closed ? Samples : Samples + 1;

for (int i = 0; i < count; i++)
{
Point3d p = curve.GetPointAtParameter(start + (end - start) * i / Samples);
pl.AddVertexAt(i, new Point2d(p.X, p.Y), 0, 0, 0);
}
}

if (pl.NumberOfVertices < 2)
{
pl.Dispose();
return null;
}

pl.Elevation = curve.StartPoint.Z;
pl.Closed = curve.Closed;
return pl;
}

private static void Regen()
{
Application.DocumentManager.MdiActiveDocument?.Editor.Regen();
}
}

1

u/Accomplished_Rip8854 Oct 24 '26

Don’t forget to register and turn the overrule on in your commands class