Author Topic: Next and Previous dwg in directory  (Read 8142 times)

0 Members and 1 Guest are viewing this topic.

GDF

  • Water Moccasin
  • Posts: 2081
Next and Previous dwg in directory
« on: March 18, 2008, 02:33:21 PM »
Tim (or anyone else can jump in here)

Ever so often when using these routines, it cases AutoCAD to lockup.
Is it because of AcadApplication.Documents.Open function (just quessing)?
I'm using AutoCAD 2008 on Windows XP

But when it does happen, it gets to be annoying. Has anyone else experienced this problem...if so, how to fix it.

I use these two routines hundreds of times a day...so I cannot live without them.

Gary


Code: [Select]
;;;by Tim Willey
(defun MyNext  (/ AcadObj DocCol DirPath DwgList Pos Dwg DwgFile ErrChk Opt)
    ; Opens/Makes-active next drawing in the directory.
  (setq AcadObj (vlax-get-Acad-Object))
  (setq DocCol (vla-get-Documents AcadObj))
  (setq DirPath (getvar "dwgprefix"))
  (setq DwgList (vl-directory-files DirPath "*.dwg" 1))
  (setq DwgList (vl-sort DwgList '<))
  (setq Pos (vl-position (getvar "dwgname") DwgList))
  (if (setq Dwg (nth (1+ Pos) DwgList))
    (progn (setq DwgFile (strcat DirPath Dwg))
           (setq ErrChk (vl-catch-all-apply 'vla-Item (list DocCol Dwg)))
           (if (vl-catch-all-error-p ErrChk)
             (progn
               (if (and (= (getvar "sdi") 1) (/= (getvar "dbmod") 0))
                 (progn (initget "Y N")
                        (setq Opt
                               (getkword
                                 "\n Drawing has changed.  Save changes before exiting drawings? <Y> "))
                        (if (or (= Opt "Y") (not Opt))
                          (setq Opt "N")
                          (setq Opt "Y"))
                        (vl-cmdf
                          "_.vbastmt"
                          (strcat "AcadApplication.Documents.Open \"" DwgFile "\""))
                        (command Opt))
                 (vl-cmdf
                   "_.vbastmt"
                   (strcat "AcadApplication.Documents.Open \"" DwgFile "\""))))
             (command
               "_.vbastmt"
               (strcat "documents.item \(\"" (vla-get-Name ErrChk) "\"\).activate"))))
    ;;(alert "\n This is the last drawing in this folder.")
    (ARCH:ALERT-I
      "MsgBox \"
     Open Next Drawing Message
--------------------------------------------------------------------------------------------
     You already are in the Last Drawing of this Directory!\"")
  )
  (princ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;by Tim Willey
(defun MyPrevious  (/ AcadObj DocCol DirPath DwgList Pos Dwg DwgFile ErrChk Opt)
    ; Opens/Makes-active previous drawing in the directory.
  (setq AcadObj (vlax-get-Acad-Object))
  (setq DocCol (vla-get-Documents AcadObj))
  (setq DirPath (getvar "dwgprefix"))
  (setq DwgList (vl-directory-files DirPath "*.dwg" 1))
  (setq DwgList (vl-sort DwgList '<))
  (setq Pos (vl-position (getvar "dwgname") DwgList))
  (if (> Pos 0)
    (progn (setq Dwg (nth (1- Pos) DwgList))
           (setq DwgFile (strcat DirPath Dwg))
           (setq ErrChk (vl-catch-all-apply 'vla-Item (list DocCol Dwg)))
           (if (vl-catch-all-error-p ErrChk)
             (progn
               (if (and (= (getvar "sdi") 1) (/= (getvar "dbmod") 0))
                 (progn (initget "Y N")
                        (setq Opt
                               (getkword
                                 "\n Drawing has changed.  Save changes before exiting drawings? <Y> "))
                        (if (or (= Opt "Y") (not Opt))
                          (setq Opt "N")
                          (setq Opt "Y"))
                        (vl-cmdf
                          "_.vbastmt"
                          (strcat "AcadApplication.Documents.Open \"" DwgFile "\""))
                        (command Opt))
                 (vl-cmdf
                   "_.vbastmt"
                   (strcat "AcadApplication.Documents.Open \"" DwgFile "\""))))
             (command
               "_.vbastmt"
               (strcat "documents.item \(\"" (vla-get-Name ErrChk) "\"\).activate"))))
    ;;(alert "\n This is the first drawing in this folder.")
    (ARCH:ALERT-I
      "MsgBox \"
     Open Next Drawing Message
--------------------------------------------------------------------------------------------
     You already are in the First Drawing of this Directory!\"")
  ) 
  (princ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Next and Previous dwg in directory
« Reply #1 on: March 18, 2008, 03:00:30 PM »
I have never had a problem with them Gary.  I will take a look when I get a moment, but then again I use '06 on a regular basis, so I might not see a problem.  I guess I might be able to run some tests on '08, as I do have it installed.  Let you know what I find out.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Next and Previous dwg in directory
« Reply #2 on: March 18, 2008, 03:07:36 PM »
I have never had a problem with them Gary.  I will take a look when I get a moment, but then again I use '06 on a regular basis, so I might not see a problem.  I guess I might be able to run some tests on '08, as I do have it installed.  Let you know what I find out.

Thanks Tim. Like I said, it only happens once in a while...always when I'm in a rush.

Gary
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Next and Previous dwg in directory
« Reply #3 on: March 18, 2008, 03:49:06 PM »
What are you opening?  And what do you have open already?

I'm just trying to narrow it down a little, as maybe it's a memory issue with loading a lot of 3d objects, and xrefs.  I just opened about ~30 drawings, going next and previous, and didn't see anything happen.  Mine had some xrefs, but nothing 3d and nothing too intense.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Next and Previous dwg in directory
« Reply #4 on: March 18, 2008, 04:22:44 PM »
What are you opening?  And what do you have open already?

I'm just trying to narrow it down a little, as maybe it's a memory issue with loading a lot of 3d objects, and xrefs.  I just opened about ~30 drawings, going next and previous, and didn't see anything happen.  Mine had some xrefs, but nothing 3d and nothing too intense.

Small to medium size unit plans...say eight files within a directory. 98% of the time the routine runs smoothly.
I checked outside and it is raining......nay that couldn't be it.

Sometimes I get this problem with Peter Jamtgaard's close drawings routine (a routine that closes all drawings selected within a dialog box).

This could be a netwok problem. Anyway thanks for checking it out for me. Others in our office have had the same problem with it locking up AutoCAD.

Like I said, I cannot work without this routine. Everyone here in the office uses it also.

Thanks again Tim.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Next and Previous dwg in directory
« Reply #5 on: March 18, 2008, 05:16:00 PM »
I wonder if it is because it is using a VBA statement.  I'm not sure, but that would be my guess.  Maybe I'll see if I can do it in C#, so that the problem won't exist, and it will be a fun little project.  :-D
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Next and Previous dwg in directory
« Reply #6 on: March 19, 2008, 10:58:35 AM »
Gary: What types of Reactors are you running?  There was a bug that got fixed - can't remember when - vlr-notification:

Code: [Select]
(vlr-notification myreactorObject 'active-document-only)
My command reactors caused havoc when opening / closing drawings until I set the above.

jb
James Buzbee
Windows 8

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Next and Previous dwg in directory
« Reply #7 on: March 19, 2008, 11:10:15 AM »
Gary: What types of Reactors are you running?  There was a bug that got fixed - can't remember when - vlr-notification:

Code: [Select]
(vlr-notification myreactorObject 'active-document-only)
My command reactors caused havoc when opening / closing drawings until I set the above.

jb
Never knew about that one James.  Thanks.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Next and Previous dwg in directory
« Reply #8 on: March 19, 2008, 12:55:18 PM »
Gary,

  Here is the C# code, and the dll file within the attached zip file.  I tested it in '08, and it seems to be working fine.  Let me know if you run into any problems.  The command names are the same once it's loaded, MyNext & MyPrevious.

  If you want them to auto load within Acad, you can see this post by Glenn that explains how to set that up.

Code: [Select]
/*
 * 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);
CompareResult = string.Compare(s1, s2);
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()
{
OpenDrawing(1);
}
[CommandMethod("MyPrevious", CommandFlags.Session)]
public void OpenPrevious()
{
OpenDrawing(-1);
}
public void OpenDrawing (int ToOpen) {

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");
Array.Sort(DwgList, new MyStringCompare1());
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;
}
}

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);
}

public class MyStringCompare1 : IComparer {
public MyStringCompare1 () {}
public int Compare (object x, object y) {
return MyStringCompare.Compare( x,y );
}
}
}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Next and Previous dwg in directory
« Reply #9 on: March 19, 2008, 02:54:25 PM »
James

Yes I have a lot of running reactors, will have to go thru them.


Tim

Thanks for the update.

Gary,

  Here is the C# code, and the dll file within the attached zip file.  I tested it in '08, and it seems to be working fine.  Let me know if you run into any problems.  The command names are the same once it's loaded, MyNext & MyPrevious.

  If you want them to auto load within Acad, you can see this post by Glenn that explains how to set that up.


I think I'm over my head. Is the following below the reg file I need to make? I renamed your test.dll to NextPrev17.dll.
My command names would be "N2" for "MyNext" and "P2" for "MyPrevious".

Code: [Select]
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409\Applications\NextPrev17]
"DESCRIPTION"="Next Previous Drawing in the Directory"
"LOADCTRLS"=dword:00000004
"LOADER"="V:\\ARCH\\Support\\V17\\NextPrev17.dll"
"MANAGED"=dword:00000001

[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409\Applications\NextPrev17\Commands]
"M2"="MyNext"
"P2"="MyPrevious"

Gary
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Next and Previous dwg in directory
« Reply #10 on: March 19, 2008, 03:18:40 PM »
That looks like what I have, except

Code: [Select]
[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409\Applications\NextPrev17\Commands]
"M2"="MyNext"
"P2"="MyPrevious"

Won't work.  I just tried it.  You have to have the values be the same on both sides (not sure why).  But once you do that you can change the pgp file to

M2, *MyNext
P2, *MyPrevious

and it will work.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Next and Previous dwg in directory
« Reply #11 on: March 19, 2008, 03:46:19 PM »
Tim

Can't get it to work. The commands are unknown: "mynext" and "myprevious".

Gary

Code: [Select]
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409\Applications\NextPrev17]
"DESCRIPTION"="Next Previous Drawing in the Directory"
"LOADCTRLS"=dword:00000004
"LOADER"="V:\\ARCH\\Support\\V18\\NextPrev17.dll"
"MANAGED"=dword:00000001

[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409\Applications\NextPrev17\Commands]
"MyNext"="MyNext"
"MyPrevious"="MyPrevious"
« Last Edit: March 19, 2008, 03:49:38 PM by Gary Fowler »
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Next and Previous dwg in directory
« Reply #12 on: March 19, 2008, 03:57:09 PM »
Try changing the LOADCTRLS to 12 instead of 4.  Mine is set to 12.  Not sure why I changed mine, so I didn't want to lead you astray earlier.

You may also have to add the Groups folder under the NextPrev17 folder.  Also you would add one string entry with both values being the same.  Mine are MyCommands, but I'm sure you can call it whatever you want.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Next and Previous dwg in directory
« Reply #13 on: March 19, 2008, 04:12:28 PM »
Try changing the LOADCTRLS to 12 instead of 4.  Mine is set to 12.  Not sure why I changed mine, so I didn't want to lead you astray earlier.

You may also have to add the Groups folder under the NextPrev17 folder.  Also you would add one string entry with both values being the same.  Mine are MyCommands, but I'm sure you can call it whatever you want.

Tim

Made the changes and still get unkown command.

Code: [Select]
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409\Applications\NextPrev17\Groups]
"MYCommands"="MYCommands"


[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409\Applications\NextPrev17]
"DESCRIPTION"="Next Previous Drawing in the Directory"
"LOADCTRLS"=dword:00000012
"LOADER"="V:\\ARCH\\Support\\V18\\NextPrev17.dll"
"MANAGED"=dword:00000001

[HKEY_CURRENT_USER\SOFTWARE\Autodesk\AutoCAD\R17.1\ACAD-6001:409\Applications\NextPrev17\Commands]
"MyNext"="MyNext"
"MyPrevious"="MyPrevious"

Gary
« Last Edit: March 19, 2008, 04:33:14 PM by Gary Fowler »
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Next and Previous dwg in directory
« Reply #14 on: March 19, 2008, 04:50:37 PM »
Did you exit and reenter Acad?  Have you tried to just netload it and use the commands?

Are you using a netword path?  If so you might want to read the post after the one I linked to before.  .Net files act differently from local and network drives.

Here is what mine looks like.  I exported it to a text file.
Quote
Key Name:          HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R16.2\ACAD-4007:409\Applications\MyAcadCommands
Class Name:        <NO CLASS>
Last Write Time:   5/15/2007 - 3:26 PM
Value 0
  Name:            DESCRIPTION
  Type:            REG_SZ
  Data:            My dotNet applications.

Value 1
  Name:            LOADCTRLS
  Type:            REG_DWORD
  Data:            0xc

Value 2
  Name:            MANAGED
  Type:            REG_DWORD
  Data:            0x1

Value 3
  Name:            LOADER
  Type:            REG_SZ
  Data:            C:\MyCustom\Dll\3MAutoCAD.dll


Key Name:          HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R16.2\ACAD-4007:409\Applications\MyAcadCommands\Commands
Class Name:        <NO CLASS>
Last Write Time:   3/19/2008 - 12:11 PM
Value 0
  Name:            MyXrefManager
  Type:            REG_SZ
  Data:            MyXrefManager

Value 1
  Name:            NewRev
  Type:            REG_SZ
  Data:            NewRev

Value 2
  Name:            MatchRevisions
  Type:            REG_SZ
  Data:            MatchRevisions

Value 3
  Name:            UpdateRevCloud
  Type:            REG_SZ
  Data:            UpdateRevCloud

Value 4
  Name:            MyBPlot
  Type:            REG_SZ
  Data:            MyBPlot

Value 5
  Name:            CopyXrefLayerProps
  Type:            REG_SZ
  Data:            CopyXrefLayerProps

Value 6
  Name:            MyAttPalette
  Type:            REG_SZ
  Data:            MyAttPalette

Value 7
  Name:            XrefCheck
  Type:            REG_SZ
  Data:            XrefCheck

Value 8
  Name:            NewUpdTitleBlock
  Type:            REG_SZ
  Data:            NewUpdTitleBlock

Value 9
  Name:            FilterForEnglishWord
  Type:            REG_SZ
  Data:            FilterForEnglishWord

Value 10
  Name:            UpdateMiddleTitleLine
  Type:            REG_SZ
  Data:            UpdateMiddleTitleLine


Key Name:          HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R16.2\ACAD-4007:409\Applications\MyAcadCommands\Groups
Class Name:        <NO CLASS>
Last Write Time:   5/15/2007 - 3:26 PM
Value 0
  Name:            MyCommands
  Type:            REG_SZ
  Data:            MyCommands
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.