Author Topic: New to BricsCADV24! Hlr Engine API  (Read 2368 times)

0 Members and 1 Guest are viewing this topic.

MickD

  • King Gator
  • Posts: 3590
  • (x-in)->[process]->(y-out) ... simples!
New to BricsCADV24! Hlr Engine API
« on: December 06, 2023, 11:04:25 PM »
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  :roll:) 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:
Code - C#: [Select]
  1.     class DCS_HlrEngine
  2.     {
  3.         public HlrCollector CreatePlanView(ObjectIdCollection ids, Point3d targetPoint, Vector3d viewDirection)
  4.         {
  5.             // control flags that determine how to process the input:
  6.             var control = HlrControl.Project | HlrControl.Subentity | HlrControl.Block | HlrControl.Cleanup | HlrControl.ShowAll | HlrControl.HideTangents;
  7.  
  8.             // for finer control you can pass in a virtual viewport with which you can
  9.             // set front and back clipping distances etc, handy for section/elevation views.
  10.             var engine = new HlrEngine(targetPoint, viewDirection, control);
  11.  
  12.             var collector = new HlrCollector(ids);
  13.  
  14.             ErrorStatus es = engine.Run(collector);
  15.             if(es != ErrorStatus.OK)
  16.             {
  17.                 // handles errors better than this, don't just return null ;)
  18.                 return null;
  19.             }
  20.             return collector;
  21.         }
  22.     }
  23.  

And a simple command to demonstrate usage:
Code - C#: [Select]
  1. [CommandMethod("hlrtest")]
  2.         public void hlrtest()
  3.         {
  4.             DocumentLock doclock = _AcAp.Application.DocumentManager.MdiActiveDocument.LockDocument();
  5.             Editor ed = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
  6.  
  7.             // Create a selection filter and get some solids:
  8.             TypedValue[] tvarray = new TypedValue[1];
  9.             tvarray.SetValue(new TypedValue((int)DxfCode.Start, "3DSOLID"), 0);
  10.  
  11.             SelectionFilter ssfilter = new SelectionFilter(tvarray);
  12.             PromptSelectionResult psr = ed.GetSelection(ssfilter);
  13.  
  14.             SelectionSet sset = null;
  15.             if (psr.Status == PromptStatus.OK)
  16.             {
  17.                 sset = psr.Value;
  18.             }
  19.             else
  20.             {
  21.                 ed.WriteMessage("\nSorry to see you go...");
  22.                 return;
  23.             }
  24.  
  25.             // fire up a transaction and run the engine:
  26.             var db = HostApplicationServices.WorkingDatabase;
  27.             using (var tr = db.TransactionManager.StartTransaction())
  28.             {
  29.                 // create and run the 2d engine:
  30.                 var engine = new DCS_HlrEngine();
  31.                 // the returned collector holds all the info you need going forward like the original entities passed in
  32.                 // and the resultant line entities with hidden and visible lines etc.
  33.                 var collector = engine.CreatePlanView(new ObjectIdCollection(sset.GetObjectIds()), Point3d.Origin, Vector3d.ZAxis);
  34.  
  35.                 // iterate the controller OutputData and add the new entities to the db:
  36.                 BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  37.                 BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],
  38.                                                 OpenMode.ForWrite) as BlockTableRecord;
  39.  
  40.                 // loop the output hlrdata objects, from the HlrData object you can get the resultant entity
  41.                 // and the 'root' or orininator entity passed in. You can use the root entity to get it's properties
  42.                 // like layer, colour etc and assign those properties to the new result entity.
  43.                 for (int i = 0; i < collector.OutputDataLength; i++)
  44.                 {
  45.                     var data = collector.OutputData(i);
  46.                     var ent = data.ResultEntity;
  47.                     // process the visibility:
  48.                     if(data.EntityVisibility == Bricscad.Hlr.Visibility.Hidden)
  49.                     {
  50.                         // add to a hidden layer with prefered linetypes etc for hidden lines
  51.                         ent.ColorIndex = 6;
  52.                     }
  53.                     else if(data.EntityVisibility == Bricscad.Hlr.Visibility.Visible)
  54.                     {
  55.                         // add to visible lines layer
  56.                         ent.ColorIndex = 1;
  57.                     } else
  58.                     {
  59.                         // you can do more checks but you get the idea.
  60.                         ent.ColorIndex = 7;
  61.                     }
  62.                     btr.AppendEntity(ent);
  63.                     tr.AddNewlyCreatedDBObject(ent, true);
  64.                 }
  65.                 tr.Commit();
  66.             }
  67.         }
  68.  

May the fun (i.e. more productive...) times begin!
« Last Edit: December 06, 2023, 11:12:56 PM by MickD »
"Short cuts make long delays,' argued Pippin.
J.R.R. Tolkien

rh-swap

  • Mosquito
  • Posts: 1
Re: New to BricsCADV24! Hlr Engine API
« Reply #1 on: January 19, 2024, 08:40:04 AM »
Thanks MickD,

I had the same failure to use c++ HLR-API in .net c# by AutoCad.

Nice to know:

- which 3D entities / 2D nets are included?
- are BlockRefs and X-Refs content included?

Ralf

MickD

  • King Gator
  • Posts: 3590
  • (x-in)->[process]->(y-out) ... simples!
Re: New to BricsCADV24! Hlr Engine API
« Reply #2 on: January 19, 2024, 01:39:39 PM »
I know that 2d linework works fine, Block linework works too but attributes do not come through. Not sure about xref's.

The code I posted filters for 3d solids, just remove the filter and see what happens :)

Welcome to The Swamp!
"Short cuts make long delays,' argued Pippin.
J.R.R. Tolkien