A while back, Jeff posted a Lisp routine for displaying available point numbers in Civil-3D. Here's a little routine that does basically the same thing, except in C#.NET.
This routine uses my
C3DUtil class that I posted previously to open a connection to Civil-3D, but the rest of the logic is all in this file.
I didn't really convert the Lisp routine, so much as just write something from scratch. So it's not a direct translation, but it does the same thing. In the process, I found something in Microsoft's SDK documentation which gave me the clue about how to call Item() methods from C#, so it turned out to be a useful routine to write, even though there's already a working Lisp version.
In the near future, I'll update our webpage with a new DLL that adds this routine to the others I've been creating.
/*
* Created by Richard Sincovec
* User: Sinc
* Date: 3/14/2007
* Time: 7:15 PM
*
*/
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AECC.Interop.Land;
using Civil3DUtilities;
namespace Sincpac.FreePointNumbers
{
/// <summary>
/// Description of FreePointNumbers.
/// </summary>
public class FreePointNumbers
{
public FreePointNumbers()
{
}
[CommandMethod("FREEPT")]
public void DisplayFreePointNumbers()
{
C3DUtil c3dUtil = new C3DUtil();
List<int> ptNums = new List<int>();
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
string freelist = "\n1+";
if (c3dUtil.AeccDb.Points.Count > 0) {
freelist = "\n";
foreach (AeccPoint point in c3dUtil.AeccDb.Points) {
if (!ptNums.Contains(point.Number)) {
ptNums.Add(point.Number);
}
}
if (ptNums.Count == 1) {
freelist += rangeBetweenNumbers(1, ptNums[0]-1);
freelist += (ptNums[0]+1).ToString() + "+";
}
if (ptNums.Count > 1) {
ptNums.Sort();
int i = 0;
freelist += rangeBetweenNumbers(1, ptNums[0]-1);
while (i < ptNums.Count-1) {
while ((i < ptNums.Count-1) && (ptNums[i+1]-ptNums[i]==1)) {
++i;
}
if (i < ptNums.Count-1)
freelist += rangeBetweenNumbers(ptNums[i]+1, ptNums[++i]-1);
}
freelist += (ptNums[i]+1).ToString() + "+";
}
}
ed.WriteMessage(freelist);
}
private string rangeBetweenNumbers(int startNum, int endNum)
{
int diff = endNum - startNum;
if (diff == 0)
return startNum.ToString() + ",";
if (diff > 0)
return startNum.ToString() + "-" + endNum.ToString() + ",";
return "";
}
}
}