Author Topic: ( C3D ) Available Point Numbers - revisited  (Read 2138 times)

0 Members and 1 Guest are viewing this topic.

sinc

  • Guest
( C3D ) Available Point Numbers - revisited
« on: March 15, 2007, 12:43:35 AM »
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.

Code: [Select]
/*
 * 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 "";
}
}
}

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: ( C3D ) Available Point Numbers - revisited
« Reply #1 on: March 15, 2007, 06:54:17 AM »
Thanks sinc, hopefully soon I'll be able to get going with C# and this code will sure help me.
TheSwamp.org  (serving the CAD community since 2003)