Jump to content

Help requested! Want a Lisp routine that loads a prefix dwg and changes layers to it


yonderbob

Recommended Posts

Greetings all,

 

I'd like to start by thanking you for taking the time to read my question and awnser it if you can.

 

Hello, i'm a lisp newbie and need the following program/Lisp routine:

 

I want it to Load a predefined dwg file where i have all the layers/attributes preset into a different drawing (the one where i want to use the routine in) and then change all the layers in this drawing to the ones i preset in the predefined Dwg. The layers in the predefined dwg file have a different name then the ones in the drawing i want to change. So i want it to link certain layers in this drawing to the one i predefined in the other. This way it is easier for me to make changes, then i only have to edit my predefined dwg and not the code.

Hope you understand what i mean and can help me with the right code/commands i need.

 

Thanks in advance.

 

bob

Link to comment
Share on other sites

Welcome to the forum yonderbob

 

I would think template (dwt) and laytrans would be enough to accomplish what you need wihtout the need for a lisp code. depending on your layer naming convention, you may need to set this up just one time

 

Play around with this and decide if you still have a need for a code

 

 

Cheers

Link to comment
Share on other sites

Thank you for your awnser pBe,

 

But i do want the lisp code, because i want to simplify things for my colleges.

I just want them to press 1 button and Done, because i will add alot more things to the code, i just want to start with this.

 

Thanks,

 

Bob

Link to comment
Share on other sites

.....

I want it to Load a predefined dwg file where i have all the layers/attributes preset into a different drawing (the one where i want to use the routine in) ....

 

Would you rather do it the other way around?

Load the target drawing into the template with the preset layes/attributes.

 

You need to remember when importing "stuff" into a existing drawing, blocks(attributes)/layers with the same name will follow the currents' , so by doing it the other way would be a better approach

 

 

.... So i want it to link certain layers in this drawing to the one i predefined in the other.

 

with a lisp code you still need to hardcode the layer names. how else would the program knows what goes where and where goes what.

 

You could achieve this with either a lisp code or laytrans

 

we could write one for you if we get a better understanding on what you want to achieve.

 

BUT first try to search for laytrans on the search button and see what you can come up then we'll pick up from there

 

Cheers

Link to comment
Share on other sites

Thanks for your awnser, but we do not want it the other way around since we already do it that way, we open the template end xref/bind/insert the drawing in the predefined dwg. We want to go away from that process and speed up Things alot with the lisp routine.

 

I do not ask of you to write the program for me i would rather write it myself so i understand what is running. So i can fix things in the future.

 

I will do the hardcoding part with all the layer names because we are talking about over 100 layers. I just want the setup so i know how to do it.

 

So in simple. I want layer 1 from drawing A to be changed To Layer 1-A in drawing B with the same atributes it has in Drawing B.

What is the code to do that because all the code i find is working with Current layer wich is not gonna work.

 

Thanks.

 

Bob

Link to comment
Share on other sites

 

I do not ask of you to write the program for me i would rather write it myself so i understand what is running. So i can fix things in the future.

 

Good for you Bob

 

So in simple. I want layer 1 from drawing A to be changed To Layer 1-A in drawing B with the same atributes it has in Drawing B.

 

Question for you:

By attributes you mean properties?

 

Are you wanting to Rename layer 1 to Layer "1A", by doing so. it will keep the properties of layer "1" i.e. color,linetype,overrides.... ?

 

or "move" all entities from layer "1" to layer "1A" and inherits the properties of layer "1A'

 

or Rename layer "1" and "reset" its properties to match "1A"?

Link to comment
Share on other sites

Yes i mean properties,

 

I want it to become layer A1, renaming is out of the question since i want drawing B to be the template like i said.

 

So i'll try to explain better, Were in Drawing A where i want to run the lisp routine. I want it to load the layers from drawing B into Drawing A and change the layers from Drawing A into the layers i got from Drawing B. So layer 1 (from drawing A) changes into layer 1-A from drawing B with every properties it has. So its the 2nd option you mentiond i gues. "or "move" all entities from layer "1" to layer "1A" and inherits the properties of layer "1A'

"

 

Thanks,

 

bob

Link to comment
Share on other sites

OK....

Heres what you need to do.

 

Layer Extraction:

Couple of ways to do that:

[Out of the Box]

Design Center: fast and easy

Layer States Manager: <_layerstate> One time export/import

[Lisp]

Write a code to extract the layers from "Drawing B" or

The Source

http://www.cadtutor.net/forum/showthread.php?54830-Layer-Extractor&highlight=layer+extractor

 

Layer Translation:

[Out of the Box]

_Laytrans

_CopyToLayer

[Lisp]

Write a code to translate Layer names

Same behavior as _Laytrans, but as you said before you need "One Button" application.

 

Comparison: you need a code to translate "what" to "where"

(hope the layer names is not as bad as i think it is because this is where it starts to get nasty).

 

* _copytolayer behavior code (which means selecting the objects with a layer filter (cons 8 "LAYER"), transfering entities from one layer to another

 

* _laytans behavior code (requries to select Layer names on a list panels. sort of renaming a layer and modifying their properties

 

In conjunction with the Layer extraction code, you might as well run it on your current drawing "Drawing A" that way you generate a list of layers to compare with in the same format as "Drawing B"

 

What i would suggest is generate a file from "Drawing A" and "Drawing B", find a match within the two list, if found modify the list of "Drawing A" maintaining the layer name

"Layer 1" properties, Color: Blue / Linetype: Hidden

"Layer 1A" properties, Color: Red / Linetype: Continuous

Change "Layer 1" to Color:Red Linetype: Continuous, move on to the next, after that re-laod the modified "Drawing A" list with the edited properties, and end it with a rename layer routine.

 

All this can be done without selecting a single object or selecting a name within a list.

Link to comment
Share on other sites

What to do when you've got 2 (or more) layers (say Layer1 & Layer2) which have to become one layer (say NewLayer). Do you need to invoke the LayMrg command? I know the LayTrans handles this internally, but I can see a very complex lisp to try and do this inside of blocks (and even then might break stuff like DB's)!

Link to comment
Share on other sites

That is what i'm afraid of Irneb. that might be a problem wherein you need to "move" them objects to a consolidated layer name

That is also why i was hoping the layer names isnt as bad as that..

 

Anyhoo.. before invoking the code he might need to run a pre-routine to consolidate layers inside "Drawing A" to make the ration 1:1 and not 2 layers to 1 or moer (hopefully not)

Link to comment
Share on other sites

.... but I can see a very complex lisp to try and do this inside of blocks (and even then might break stuff like DB's)!

 

Good point ..

That is why as a general rule, Entities inside Blocks should be created in "0" layer so it would inherit the layer it resides to.

Link to comment
Share on other sites

  • 2 weeks later...

Thank you for your awnsers,

I have tried to pull something off but came to the conclusion that what i want to acomplish can be done easier.

Now i want to Import the layers from Drawing B into drawing A and then Change the objects from a certain layer to the newly imported layers.

There will be no issue with conflicts with layer names as both files will have different layer names. Then after the objects changed i want to delete the old layers from Drawing A.

I would like this all in 1button lisp code.

If you could supply me with an example code how to import layers from 1 drawing and then the code how to move objects from 1 layer to another. I think then i would have enough info to build the code.

If you could please give me an example code and not just 1 command since i'm a nooby in lisp and would not have enough knowledge to troubleshoot all extra codes that are needed with 1 command.

That would help me alot.

 

Thanks in advance,

 

Bob

Link to comment
Share on other sites

To extract Layers from another drawing, you might want to have a look at Lee Mac's "Steal from Drawing".

 

As for assigning entities to the new layers, the "easiest" way would be to step through each block definition (note modelspace and all paperspaces are also blocks). This you can do through the vla methods (i.e. ActiveX/COM similar to what you'd have done in VBA). Something like the following:

(vlax-for block (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
 ;; Your code to run for each block definition in turn
 
 (vlax-for entity block ;Run through each entity inside the current block
   (if (eq (vla-get-Layer entity) "OldLayerName") ;Check if the entity is on the old layer
     (vla-put-Layer entity "NewLayerName") ;If so then set it to the new layer
   )
 ) ;End of vlax-for (on entities) loop

 ;; Any other code you want to run on the block definition after changing the entities
) ;End of vlax-for (on blocks) loop

What I've shown is very simplistic, it only handles one Old & New layername pair. To be able to have a list of old & new names, you might want to save such to a file so you can more easily edit them. Best and easiest choice would be to use Excel and save to a TAB delimited file (have string delimiter = ") having the OldLayerName in column A and the NewName in B, Then the following would read such file into a list:

(setq lst nil) ;Ensure the list is empty
(if (setq f (open "C:/Path/Filename.TXT" "r")) ;If file can be opened for read
 (progn ;Groung all the next items as the then portion
   (while (setq str (read-line f))
     (setq lst (cons (read (strcat "(" (strcase str) ")")) lst))
   )
   (close f) ;Close the file
   lst ;Return the list
 )
)

Now you should have an association list, which you can use assoc on with the old layer name. This would then give you a list containing 2 elements so you can use cadr on it to get the 2nd (i.e. the new name). Remember though, assoc is case sensitive. That's why I placed the strcase in the code. You'd need it on the entity's layer name as well to use in assoc.

Link to comment
Share on other sites

Hi,

 

I've come up with this code so far.

I used lee's steal code to get all the layers and that works good.

But the code to change objects to a different layer doesn't work.

Autocad doesn't show an error either, it just doesn't change the objects.

Could anyone help me with what im forgetting/doing wrong.

 

Thanks in advance bob

 

 

 
;--------------------------------------------------------------------------------
;    Load Drawing function    ;
(defun C:Thecleaner ()
(Steal "S:\\Joulz\\SP\\Haaglanden\\Bedrijfsbureau\\Tekenkamer\\Etienne\\Lisp\\standaardtekeningelektra.dwg"
 '(
    ("Layers"
      "E_B_kast"
"EIGEN_TOPO_LIJN"
"E_B_BESCHERMING"
"E_B_DIEPTE"
"E_B_DWARSPROFIEL"
"E_B_KAST"
"E_B_KV04_AANSLUITAFTAKMOF"
"E_B_KV04_AANSLUITKABEL"
"E_B_KV04_AANSLUITMOF"
"E_B_KV04_KABEL"
"E_B_KV04_KABEL_LABEL_VB0"
 "E_B_KV04_KABEL_LOCATIE_VB"
"E_B_KV04_LEVERINGSPUNT_PRI"
"E_B_KV04_OV_MOF"
"E_B_KV10_KABEL_LABEL_VB"
"E_B_KV10_KABEL_LOCATIE_VB"
"E_B_KV23_KABEL_LABEL_VB"
"E_B_KV23_KABEL_LOCATIE_VB"
"E_B_KV25_KABEL"
"E_B_KV25_KABEL_LABEL_VB"
"E_B_KV25_KABEL_LOCATIE_VB"
"E_B_KV3_50_MOF"
"E_B_KV3_KABEL"
"E_B_KV3_KABEL_LABEL_VB"
"E_B_KV3_KABEL_LOCATIE_VB"
"E_N_HS_KABEL"
"E_N_HS_LABEL"
"E_N_HS_MATEN"
"E_N_HS_TEKST"
"E_N_LS_DWARSPROFIEL"
"E_N_LS_KABEL" 
"E_N_LS_LABEL"
"E_N_LS_MATEN"
"E_N_LS_TEKST"
"E_N_MS_DWARSPROFIEL"
"E_N_MS_KABEL"
"E_N_MS_LABEL"
"E_N_MS_MATEN"
"E_N_MS_TEKST"
"E_N_OV_DWARSPROFIEL"
"E_N_OV_KABEL"
"E_N_OV_LABEL"
"E_N_OV_MATEN"
"E_N_OV_TEKST"
"E_N_TC_DWARSPROFIEL"
"E_N_TC_KABEL"
"E_N_TC_LABEL"
"E_N_TC_MATEN"
"E_N_TC_TEKST"
"E_B_OVERLENGTE"
"E_B_OV_AANSLUITAFTAKMOF"
"E_B_OV_AANSLUITKABEL"
"E_B_OV_AANSLUITMOF"
"E_B_OV_KABEL"
"E_B_OV_KABEL_LABEL_VB"
"E_B_OV_KABEL_LOCATIE_VB"
"E_B_OV_LEVERINGSPUNT"
"E_B_STATION"
"E_B_STATION_LIGGING_VB"
"E_TC_KABEL"
"E_TC_KABEL_LABEL_VB"
"E_TC_KABEL_LOCATIE_VB"
"E_TC_MOF"
"E_V_HS_DWARSPROFIEL"
"E_V_HS_KABEL"
"E_V_HS_LABEL"
"E_V_HS_MATEN"
"E_V_HS_TEKST"
"E_V_LS_DWARSPROFIEL"
"E_V_LS_KABEL"
"E_V_LS_LABEL"
"E_V_LS_MATEN"
"E_V_LS_TEKST"
"E_V_MS_DWARSPROFIEL"
"E_V_MS_KABEL"
"E_V_MS_LABEL"
"E_V_MS_MATEN"
"E_V_MS_TEKST"
"E_V_OV_DWARSPROFIEL"
"E_V_OV_KABEL"
"E_V_OV_LABEL"
"E_V_OV_MATEN"
"E_V_OV_TEKST"
"E_V_TC_DWARSPROFIEL"
"E_V_TC_KABEL"
"E_V_TC_LABEL"
"E_V_TC_MATEN"
"E_V_TC_TEKST"
"NEN1878_LINE"
"NEN1878_POINT"
"NEN1878_STRAAT_LABEL"
"NEN1878_TEXT"
"PLANTOPO_LIJN"
"PLANTOPO_TEKST"
     
     );end layers
    ("Blocks"
 "FLATKAST"
 "LSKAST"
 "MANTELBUIS"
 "MOF"
 "OV KAST"
 "TRAFOSTATION"
 "onderhoek elec 3-11-2009"
 
 );end blocks
  );end options
);end steal
);end defun2
;--------------------------------------------------------------------------------
;   Object from Olayer to Nlayer    ;
(vlax-for block (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
 ;; Your code to run for each block definition in turn
 
 (vlax-for entity block ;Run through each entity inside the current block
   (if (eq (vla-get-Layer entity) "E_KV04_Kabel") ;Check if the entity is on the old layer
     (vla-put-Layer entity "E_B_KV04_kabel") ;If so then set it to the new layer
   )
 ) ;End of vlax-for (on entities) loop
 ;; Any other code you want to run on the block definition after changing the entities
) ;End of vlax-for (on blocks) loop
;--------------------------------------------------------------------------------

;--------------------------------------------------------------------------------

Link to comment
Share on other sites

I've come up with a different problem. I want to change the text heigh of text in 1 particular layer in the same code.

My code runs perfect now. It loads the layers and changes the layers to the new layers with the new properties.

But after that i want to code to change the text height and the text font of the text in 1 layer. I tried with the Vlax command for layer change but i failed and my code crashed :(.

could anyone help me and supply me with the right commands/codes to do that.

 

Thanks in advance.

 

bob

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...