Author Topic: Macro to Rename Xref  (Read 3491 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Macro to Rename Xref
« on: February 26, 2015, 01:56:43 PM »
I am looking for a method that via command prompt or lisp, that I can get an xref to rename to a generic name, and then load a .las file.

I guess the thought is, if I have a ABCD.1234.SH.Siteb.dwg, that I can rename that to .SH.S, then import the .LAS.
(I have made a .LAS that I exported the Layer State Out using the .SH.S) I figured I could rename the project xref to a generic name then import the layer state, activate the layer state, then be done).

I do have a slight variation of naming with this type of file, it could be either:

ABCD.5678.SH.Siteb.dwg
ABCD.1234.SH.SB.dwg
ABCD.4567.SH.Sitereference.dwg

I did not know if maybe a the user could pick the Xref in the drawing, that would then be renamed to the generic name and go from there.

Just a brain storm... Thanks for any ideas.


Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Macro to Rename Xref
« Reply #1 on: February 26, 2015, 02:11:45 PM »
...
I guess the thought is, if I have a ABCD.1234.SH.Siteb.dwg, that I can rename that to .SH.S, then import the .LAS.
...
What does this accomplish ?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro to Rename Xref
« Reply #2 on: February 26, 2015, 02:29:15 PM »
I have created layer states for demo plan, site plan, utility plan.

I have a proposed base file named similar to the above. I then in my proposed base file turn on and change colors to layers that I want to see in the utility plan. Once I get it to look the way I want. I save the file and then Xref that proposed base file into a clean dwg. So if the file name is ABCD.1234.SH.Siteb.dwg. I go to the manager and rename it to .SH.S. I open the layer manager then create a layer state called proposed base for utility plan.

The idea is that I can make this a generic layer state for the proposed base to be loaded into any new or old project (utility plan). So if I were to open an older project's Utility Plan. the proposed base name is this ABCD.5678.SH.SB.dwg. I can then rename that to be .SH.S , Open the Layer State manager and import the proposed base for utility plan in, then activate it. Changes everything to the correct colors and on off layers.

I hopefully explained it as clear as I can. I can manually do all this, so the process does work. Just wanting a way to kinda automate it.
Civil3D 2020

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Macro to Rename Xref
« Reply #3 on: February 26, 2015, 02:59:42 PM »
Do you mean something along these lines:
Code: [Select]
(vlax-for blockDefObj (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
  (if (= (vla-get-isxref blockDefObj) :vlax-true)
    (cond
      ((wcmatch (setq oldName (vla-get-name blockDefObj)) "*ABC")
        (if (tblobjname "block" "ABC")
          (princ "\Error: ABC already exists ")
          (progn
            (vla-put-name blockDefObj "ABC")
            (layerstate-import "ABC.las")
            (vla-put-name blockDefObj oldName)
          )
        )
      )
      ((wcmatch oldName "*DEF")
        ...
      )
      ((wcmatch oldName "*GHI")
        ...
      )
    )
  )
)

EDIT: Replaced foreach with vlax-for as per ronjonp's remark.
« Last Edit: March 02, 2015, 08:04:50 AM by roy_043 »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Macro to Rename Xref
« Reply #4 on: February 26, 2015, 03:21:26 PM »
Oh .. I get it now after looking at Roy's code ( takes some a bit longer ) :)
Here's a base using the three example layerstates in the post above.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ demo ls_path oldname site util)
  2.   ;; Assumes that these are your file naming conventions for the relevent xref
  3.   ;; Searches for a wildcard match *site* and renames accordingly
  4.   (setq ;; LAS file needs to be named ".SH.S.las"
  5.         site    ".SH.S"
  6.         ;; LAS file needs to be named ".SH.D.las"
  7.         demo    ".SH.D"
  8.         ;; LAS file needs to be named ".SH.U.las"
  9.         util    ".SH.U"
  10.         ;; DUH :)
  11.         ls_path "C:\\MyPath\\To\\Saved\\LayerStates\\"
  12.   )
  13.     (if (= (vla-get-isxref bdf) :vlax-true)
  14.       (cond ;; Check that we have a wildcrad match
  15.             ((wcmatch (strcase (setq oldname (vla-get-name bdf))) (strcat "*" site "*"))
  16.              (if (not (tblobjname "block" (substr site 1 5)))
  17.                (progn ;; Put name to ".SH.S"
  18.                       (vla-put-name bdf (substr site 1 5))
  19.                       ;; Import the already saved layerstate
  20.                       (layerstate-import (strcat ls_path (substr site 1 5) ".las"))
  21.                       ;; Restore the layerstate
  22.                       (layerstate-restore (substr site 1 5) nil 1)
  23.                       ;; Restore xref name
  24.                       (vla-put-name bdf oldname)
  25.                )
  26.              )
  27.             )
  28.             ((wcmatch (strcase (setq oldname (vla-get-name bdf))) (strcat "*" demo "*"))
  29.              (if (not (tblobjname "block" (substr demo 1 5)))
  30.                (progn (vla-put-name bdf (substr demo 1 5))
  31.                       (layerstate-import (strcat ls_path (substr demo 1 5) ".las"))
  32.                       (layerstate-restore (substr demo 1 5) nil 1)
  33.                       (vla-put-name bdf oldname)
  34.                )
  35.              )
  36.             )
  37.             ((wcmatch (strcase (setq oldname (vla-get-name bdf))) (strcat "*" util "*"))
  38.              (if (not (tblobjname "block" (substr util 1 5)))
  39.                (progn (vla-put-name bdf (substr util 1 5))
  40.                       (layerstate-import (strcat ls_path (substr util 1 5) ".las"))
  41.                       (layerstate-restore (substr util 1 5) nil 1)
  42.                       (vla-put-name bdf oldname)
  43.                )
  44.              )
  45.             )
  46.       )
  47.     )
  48.   )
  49.   (princ)
  50. )
« Last Edit: February 26, 2015, 04:39:30 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Macro to Rename Xref
« Reply #5 on: February 26, 2015, 03:54:49 PM »
Updated code above to use VLAX-FOR vs. FOREACH  :oops:  ( Roy you need to change yours ).
*Added some comment above so OP can troubleshoot.
« Last Edit: February 26, 2015, 04:09:49 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro to Rename Xref
« Reply #6 on: February 26, 2015, 04:34:11 PM »
Awesome. Thanks for the help! Here is what I am doing.

I have a C:\work\layer_states\ (folder created)

I have the .SH.S.las, .SH.D.las, and the .SH.U.las.

I have xref'd a project proposed base. then in the xref manager renamed it to .SH.XREF.S.

I load the lisp and type foo. and I think I broke it.

Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Macro to Rename Xref
« Reply #7 on: February 26, 2015, 04:40:46 PM »
Copy the code above again .. had a typo .. hopefully it works as I'm coding blindly ( no test files )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Macro to Rename Xref
« Reply #8 on: February 26, 2015, 04:45:21 PM »
..
I have xref'd a project proposed base. then in the xref manager renamed it to .SH.XREF.S.
..
Now you've lost me. The code should rename the xref before it imports and restores the layer state.
Per your examples, I assumed that your naming convention was this:
*.SH.S* = Site Plans
*.SH.D* = Demo
*.SH.U* = Utility

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro to Rename Xref
« Reply #9 on: February 26, 2015, 05:10:11 PM »
No. You are right what you just stated. I just can not seem to get it to work.
Civil3D 2020

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro to Rename Xref
« Reply #10 on: February 26, 2015, 05:21:15 PM »
Ok... I have rebooted and still am not having any luck. This is just a thought but I do not need to select the xref first to apply the layer state too or do I?
Civil3D 2020

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Macro to Rename Xref
« Reply #11 on: March 02, 2015, 08:06:09 AM »
Updated code above to use VLAX-FOR vs. FOREACH  :oops:  ( Roy you need to change yours ).
Code updated. Good catch ronjonp.