Was a really simple fix. Edited one line in the sort function to ignore case, and then recompile.
Edit: Forgot to mention that I added two other functions, so now you can close the current drawing when switching to the next/previous drawing.
/*
* Created by SharpDevelop.
* User: Tim Willey
* Date: 3/18/2008
* Time: 4:17 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Collections;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass (typeof (Test.OpenNextPrevious))]
namespace Test
{
/// <summary>
/// Description of OpenNextPrevious.
/// </summary>
class MyStringCompare {
public MyStringCompare () {}
public static int Compare (object obj1, object obj2) {
string str1 = (string)obj1, str2 = (string)obj2;
int i1 = 0,
i2 = 0,
CompareResult,
l1 = str1.Length,
l2 = str2.Length,
tempI1,
tempI2;
string s1, s2;
bool b1, b2;
while (true) {
b1 = Char.IsDigit(str1, i1);
b2 = Char.IsDigit(str2, i2);
if ( !b1 && b2 )
return -1;
if ( b1 && !b2 )
return 1;
if (b1 && b2) {
FindLastDigit(str1, ref i1, out s1);
FindLastDigit(str2, ref i2, out s2);
tempI1 = Convert.ToInt32(s1);
tempI2 = Convert.ToInt32(s2);
if ( tempI1.Equals(tempI2) )
CompareResult = 0;
else if (tempI1 < tempI2)
CompareResult = -1;
else
CompareResult = 1;
if ( !CompareResult.Equals(0) )
return CompareResult;
}
else {
FindLastLetter(str1, ref i1, out s1);
FindLastLetter(str2, ref i2, out s2);
[color=red]CompareResult = string.Compare(s1, s2, true);[/color]
if ( !CompareResult.Equals(0) )
return CompareResult;
}
if (l1 <= i1) {
if (l2 <= i2) {
return 0;
}
else {
return -1;
}
}
if (l2 <= i2) {
if (l1 < i1) {
return 0;
}
else {
return 1;
}
}
}
}
private static void FindLastLetter (string MainStr, ref int i, out string OutStr) {
int StartPos = i;
int StrLen = MainStr.Length;
++i;
while ( i < StrLen && !Char.IsDigit(MainStr, i) )
++i;
OutStr = MainStr.Substring(StartPos, i - StartPos);
}
private static void FindLastDigit (string MainStr, ref int i, out string OutStr) {
int StartPos = i;
int StrLen = MainStr.Length;
++i;
while ( i < StrLen && Char.IsDigit(MainStr, i) )
++i;
OutStr = MainStr.Substring(StartPos, i - StartPos);
}
}
public class OpenNextPrevious
{
[CommandMethod("MyNext", CommandFlags.Session)]
public void OpenNext()
{
string DwgPath = AcadApp.GetSystemVariable("DwgPrefix") as string;
string DwgName = AcadApp.GetSystemVariable("DwgName") as string;
OpenDrawing(1, DwgPath, DwgName, false);
}
[CommandMethod("MyNextAndCloseCurrent", CommandFlags.Session)]
public void OpenNextAndCloseCurrent()
{
string DwgPath = AcadApp.GetSystemVariable("DwgPrefix") as string;
string DwgName = AcadApp.GetSystemVariable("DwgName") as string;
//AcadApp.DocumentManager.MdiActiveDocument.CloseAndDiscard();
OpenDrawing(1, DwgPath, DwgName, true);
}
[CommandMethod("MyPrevious", CommandFlags.Session)]
public void OpenPrevious()
{
string DwgPath = AcadApp.GetSystemVariable("DwgPrefix") as string;
string DwgName = AcadApp.GetSystemVariable("DwgName") as string;
OpenDrawing(-1, DwgPath, DwgName, false);
}
[CommandMethod("MyPreviousAndCloseCurrent", CommandFlags.Session)]
public void OpenPreviousAndCloseCurrent()
{
string DwgPath = AcadApp.GetSystemVariable("DwgPrefix") as string;
string DwgName = AcadApp.GetSystemVariable("DwgName") as string;
//AcadApp.DocumentManager.MdiActiveDocument.CloseAndDiscard();
OpenDrawing(-1, DwgPath, DwgName, true);
}
public void OpenDrawing (int ToOpen, string DwgPath, string DwgName, bool CloseCurrent) {
Document Doc = null;
DocumentCollection DocCol = AcadApp.DocumentManager;
//string DwgPath = AcadApp.GetSystemVariable("DwgPrefix") as string;
//string DwgName = AcadApp.GetSystemVariable("DwgName") as string;
string FullPath = DwgPath + DwgName;
string[] DwgList = Directory.GetFiles(DwgPath, "*.dwg");
MyStringCompare1 msc1 = new MyStringCompare1();
Array.Sort(DwgList, msc1);
int cNum = Array.IndexOf(DwgList, FullPath);
if ( ToOpen.Equals(1) ) {
if ( cNum.Equals(DwgList.Length - 1) ) {
MessageBox.Show("Already at the last drawing in the folder.", "Not really an error.");
return;
}
}
else {
if ( cNum.Equals(0) ) {
MessageBox.Show("Already at the first drawing in the folder.", "Not really an error.");
return;
}
}
if ( CloseCurrent )
AcadApp.DocumentManager.MdiActiveDocument.CloseAndDiscard();
string Path = DwgList[cNum + ToOpen];
foreach (Document doc in DocCol) {
if (string.Compare(Path, doc.Name, true).Equals(0)) {
Doc = doc;
break;
}
}
if (Doc != null)
DocCol.MdiActiveDocument = Doc;
else
DocCol.Open(Path, false);
}
}
}