Thanks to the fine people at Bricsys they have finally released the Hlr API in in BricsCAD V24 for .Net!
I've tried a few times to do this myself with varying success (mostly un-success-ful
) but now I can rest
There's little documentation on how to use the API, even in the ObjectARX examples they assume a lot but after working with it over the years I have the basics down and can share a basic example in C# to get you producing 2d views from your models quickly.
Using a virtual viewport is the most powerful way to use the engine, with viewports you can finetune the depth of view etc by setting the clipping planes for creating things like elevations or section cuts.
Create a simple class to do the work:
class DCS_HlrEngine
{
public HlrCollector CreatePlanView(ObjectIdCollection ids, Point3d targetPoint, Vector3d viewDirection)
{
// control flags that determine how to process the input:
var control = HlrControl.Project | HlrControl.Subentity | HlrControl.Block | HlrControl.Cleanup | HlrControl.ShowAll | HlrControl.HideTangents;
// for finer control you can pass in a virtual viewport with which you can
// set front and back clipping distances etc, handy for section/elevation views.
var engine
= new HlrEngine
(targetPoint, viewDirection, control
);
var collector
= new HlrCollector
(ids
);
ErrorStatus es = engine.Run(collector);
if(es != ErrorStatus.OK)
{
// handles errors better than this, don't just return null ;)
return null;
}
return collector;
}
}
And a simple command to demonstrate usage:
[CommandMethod("hlrtest")]
public void hlrtest()
{
DocumentLock doclock = _AcAp.Application.DocumentManager.MdiActiveDocument.LockDocument();
Editor ed = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
// Create a selection filter and get some solids:
TypedValue
[] tvarray
= new TypedValue
[1]; tvarray
.SetValue(new TypedValue
((int)DxfCode
.Start,
"3DSOLID"),
0);
SelectionFilter ssfilter
= new SelectionFilter
(tvarray
); PromptSelectionResult psr = ed.GetSelection(ssfilter);
SelectionSet sset = null;
if (psr.Status == PromptStatus.OK)
{
sset = psr.Value;
}
else
{
ed.WriteMessage("\nSorry to see you go...");
return;
}
// fire up a transaction and run the engine:
var db = HostApplicationServices.WorkingDatabase;
using (var tr = db.TransactionManager.StartTransaction())
{
// create and run the 2d engine:
var engine
= new DCS_HlrEngine
(); // the returned collector holds all the info you need going forward like the original entities passed in
// and the resultant line entities with hidden and visible lines etc.
var collector
= engine
.CreatePlanView(new ObjectIdCollection
(sset
.GetObjectIds()), Point3d
.Origin, Vector3d
.ZAxis);
// iterate the controller OutputData and add the new entities to the db:
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// loop the output hlrdata objects, from the HlrData object you can get the resultant entity
// and the 'root' or orininator entity passed in. You can use the root entity to get it's properties
// like layer, colour etc and assign those properties to the new result entity.
for (int i = 0; i < collector.OutputDataLength; i++)
{
var data = collector.OutputData(i);
var ent = data.ResultEntity;
// process the visibility:
if(data.EntityVisibility == Bricscad.Hlr.Visibility.Hidden)
{
// add to a hidden layer with prefered linetypes etc for hidden lines
ent.ColorIndex = 6;
}
else if(data.EntityVisibility == Bricscad.Hlr.Visibility.Visible)
{
// add to visible lines layer
ent.ColorIndex = 1;
} else
{
// you can do more checks but you get the idea.
ent.ColorIndex = 7;
}
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
tr.Commit();
}
}
May the fun (i.e. more productive...) times begin!