Using LINQ with Civil 3D ActiveX objects

By Augusto Goncalves

Yes it is possible use .NET LINQ (Language-Integrated Query) features with Civil 3D ActiveX objects, this Microsoft technology consists in a set of extensions to .NET languages and supporting libraries that allow users to formulate queries within the programming language itself.

We can use it for a Civil 3D ActiveX collection like points, the following code sample demonstrate it. It can also be used for .NET collections in a similar way, but the new Point .NET API on Civil 3D 2013 have better features.

private Autodesk.AECC.Interop.UiLand.AeccApplication aeccApp;

private Autodesk.AECC.Interop.UiLand.AeccDocument aeccDoc;

private Autodesk.AECC.Interop.Land.AeccDatabase aeccDb;

 

//Get Civil 3D objects

public void getCivilObjects()

{

  AcadApplication oApp = (AcadApplication)

                          Application.AcadApplication;

  string sAppName = "AeccXUiLand.AeccApplication.9.0";

  aeccApp = (AeccApplication)oApp.GetInterfaceObject(sAppName);

  aeccDoc = (AeccDocument)aeccApp.ActiveDocument;

  aeccDb = (AeccDatabase)aeccDoc.Database;

}

 

//release Civil 3D objects

public void releaseCivilObjects()

{

  Marshal.ReleaseComObject(aeccDb);

  Marshal.ReleaseComObject(aeccDoc);

  Marshal.ReleaseComObject(aeccApp);

}

 

public AeccPoint[] findPoints(double easting, double northing, double tolerance)

{

  var pointLinqSearch = from AeccPoint pto in aeccDb.Points

     where

     //easting with tolerance

     (pto.Easting >= easting – tolerance &&

     pto.Easting <= easting + tolerance) &&

     //northing with tolerance

     (pto.Northing >= northing – tolerance &&

     pto.Northing <= northing + tolerance)

     select pto;

 

  return pointLinqSearch.ToArray();

}

 

[CommandMethod("findpto")]

public void testFindPoint()

{

  getCivilObjects();

 

  Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

  double easting, northing, tolerance;

  easting = northing = tolerance = 0.0;

 

  for (int i = 0; i < 3; i++)

  {

    PromptDoubleResult pdr = null;

    PromptDoubleOptions o;

    switch (i)

    {

      case 0: pdr = ed.GetDouble("Enter easting: "); break;

      case 1: pdr = ed.GetDouble("Enter northing: "); break;

      case 2: pdr = ed.GetDouble("Enter tolerance: "); break;

    }

    if (pdr.Status != PromptStatus.OK) return;

    switch (i)

    {

      case 0: easting = pdr.Value; break;

      case 1: northing = pdr.Value; break;

      case 2: tolerance = pdr.Value; break;

    }

  }

 

  AeccPoint[] ptos = findPoints(easting, northing, tolerance);

 

  foreach (AeccPoint pto in ptos)

  {

    pto.Highlight(true);

  }

 

  releaseCivilObjects();

}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading