Cadalyst - Harry's Code Class: Tips for Programmers Article .lefttext { font-size: 12px; color: #336699; font-family: Arial, Helvetica, sans-serif; font-weight: normal; line-height: 17px; margin-bottom: 10px; margin-top: 10px; } .articlehead { font-size: 17px; color: #003366; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 22px; margin: 12px 0px 5px; font-weight: bold; } { font-size: 13px; font-family: Arial, Helvetica, sans-serif; line-height: 16px; margin-top: 0px; margin-right: 7px; color: #333333; margin-bottom: 10px; } .basictext { font-size: 13px; font-family: Arial, Helvetica, sans-serif; line-height: 16px; margin-top: 9px; margin-right: 7px; color: #333333; margin-bottom: 10px; } .orange { color: #FF6600; } .searchhead { font-family: Arial, Helvetica, sans-serif; font-size: 13px; line-height: 13px; font-weight: bold; color: #CC0000; none; font-variant: normal; margin-bottom: 3px; margin-top: 9px; } body { margin-left: 0px; margin-top: 0px; } .AcctMgmtText { font-size: 11px; font-family: Arial, Helvetica, sans-serif; line-height: 14px; margin-top: 5px; color: #333333; margin-bottom: 9px; } .AcctMgmtHead { font-size: 13px; font-family: Arial, Helvetica, sans-serif; line-height: 16px; margin-top: 0px; color: #FF6600; margin-bottom: 9px; font-weight: bold; none; none; none; none; } a:visited { color: #336699; } .date {font-size: 12px; font-family: Arial, Helvetica, sans-serif; color: #000000; margin-top: 10px; line-height: 13px; margin-bottom: 9px; font-weight: normal; } .whatsnewhead {font-size: 17px; color: #FF6600; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 22px; margin: 9px 0px 5px; font-weight: bold; } .blue003366 {font-size: 12px; color: #003366; font-family: Arial, Helvetica, sans-serif; font-weight: bold; line-height: 16px; margin-bottom: 0px; } a:link { color: #336699; } .aboutauthor { font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-style: italic; line-height: 16px; color: #666666; } .articlebhead { color: #336699; font-weight: bold; font-size: 14px; } .redCadalystcom { font-size: 13px; color: #CC0000; font-weight: bold; line-height: 14px; } .style20 { font-family: Arial, Helvetica, sans-serif; font-size: 10px; } body,td,th { font-family: Arial, Helvetica, sans-serif; } .red { color: #CC0000; } .broughtby { font-size: 11px; font-style: italic; } .style24 { font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; font-size: 12px; } .style25 {font-size: 13px; line-height: 16px; color: #666666; font-family: Arial, Helvetica, sans-serif;} .style27 {font-size: 11px; color: #333333; font-family: Arial, Helvetica, sans-serif; font-weight: normal; line-height: 17px; margin-bottom: 10px; margin-top: 10px; font-style: italic; } .style38 {font-size: 13px} .style39 {color: #336699; font-weight: bold; font-size: 13px; } .caption pre { font-family: Courier New, Courier, mono; font-size: 9pt; font-weight: normal; color: #333333; } .harry-code { font-family: "Courier New", Courier, monospace; font-size: 11px; margin-left: 30px } /* styles for forum code excerpt */ /* Font Definitions */ @font-face panose-1:2 11 6 4 3 5 4 4 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; New Roman";} p.Code, li.Code, div.Code {margin:0in; @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in;} div.Section1 .style40 {font-size: 11px; font-family: Arial, Helvetica, sans-serif; line-height: 16px; margin-top: 9px; margin-right: 7px; color: #333333; margin-bottom: 10px; } --> If this article doesn't display correctly in your e-mail browser, click here to link to the online archives. To ensure successful delivery of your Cadalyst e-articles, add to your Address Book. Refer to our Safe Senders instructions for details. March 2008 Search Cadalyst.com: Cadalyst.com Harry's Library of AutoCAD Tips Hang Out with Hot Tip Harry • "Hot Tip Harry" Column • Discussion Forums • Harry's Code Class Archives Buy online • Cadalyst Articles • Cadalyst Magazine • Account Management Contact • Advertising • Editorial Messing with Layer Names =09 Today's lesson demonstrates how to create an object-oriented Visual LISP routine that processes multiple AutoCAD drawings. =09 In Cadalyst's Hot Tip Harry-Requests Discussion Forum recently, a thread about converting layer names in drawings raised some ideas I'd like to examine further. AutoCAD contains the tools to rename layers manually; however, this user hoped to automate the task so he could convert a large number of drawings flowing in from external sources. I had the notion that this was a case in which the typical AutoLISP approach of mimicking manual procedures by wrapping LISP code around commands might not be the best solution. A Word About Tables AutoCAD has two types of tables: table entity objects and drawing tables. I wrote about how to use VBA programming to create table entity objects in a previous article article, "Get Your Data in a Row (and Column)." Today's article digs into drawing tables. We explore drawing tables where you can find the information about items common amongst multiple entities. Tables save space in a drawing because common information can be referenced using an index or unique name. Tables can also be used to define nongraphical information in an ordered fashion, such as saved views. AutoCAD drawings contain tables for layers, linetypes, text styles, stored views, blocks, named coordinate systems, application IDs, dimension styles, and viewports. Online examples include: Layer Manipulations, an example of layer swap code from Bill Kramer's Autocode.com Web site. Massive Layer Renaming Program, a thread in Cadalyst's Hot Tip Harry-Requests Discussion Forum about mass layer changes. LISPS and Layers, a thread in Cadalyst's Hot Tip Harry-Help! Discussion Forum about refined layer changes. Examining the Usual Approach To begin, let's look at what is involved in manually converting layer names in AutoCAD. For each drawing in the library to be converted, the first step would be to open the drawing and look at the existing layers. Using the Layer dialog box, it is pretty easy to change names. When the dialog box process completes, all associated entities are on the newly named layer. The problem is that we can't drive an AutoCAD dialog box using VBA nor Visual LISP, so we need to look at the "-" command option. Using a hyphen (-) in front of a command name tells AutoCAD you'd like to run the command line version of the command rather than the dialog box version. Command line versions can be driven using VBA and Visual LISP programming. Problem solved, right? Well, take a look at the results of the Layer command test using the hyphen. Command: -layer Current layer: "0" Enter an option Apparently the rename functionality found in the dialog box is not part of the standard Layer command. Instead, we can conclude that another command handles renaming layers (as well as other named objects in AutoCAD): Rename. A Layer by any Other Name So now we turn our attention to the Rename command. We can rename layers by repeated calls to the -Rename command (hyphen required in the code). It's pretty easy to use the -Rename command to write a routine that renames a group of layers. Create a nested list of layer names (("old" "new")=85) at the start of the function. The routine will use this list in a loop, running the -Rename command over and over. (defun C:RENLYRS (/ LYRS LYR) (setq LYRS (list (list "LAYER1" "L1") (list "LAYER2" "L2"))) (foreach LYR LYRS (if (tblsearch "LAYER" (car LYR)) (command "-RENAME" "LA" (car LYR) (cadr LYR))))) This function uses the tblsearch subr to make sure the layer is known in the layer table before the -Rename command is issued. If the layer is not in the drawing, an error can result, causing the command sequence to get confused. As you can see in this example, the old school AutoLISP methods for accessing table data are quite powerful. They allow quick and easy access to the table data sequentially or by using the name of an entry, as in this case. Coupled with the AutoCAD command system, the table access can pretty much accomplish most application requirements. But I Have More than One Drawing! However, this old school approach can only work on your currently active drawing and requires some rethinking if you want to run a routine on a group of drawings. The old school solution is to use a script file (SCR), which is a text-based file of an AutoCAD command. Using a script, one can program AutoCAD to open each drawing, then load and run the LISP code. The following lines demonstrate what might be found in a script file that opens and processes two drawings named DEMO1 and DEMO2. Open demo1 (load "RENLYRS") RENLYRS qSave close Open Demo2 (load "RENLYRS") RENLYRS qSave close This approach works well as a one-time solution to process a limited number of drawings. It may not work well for a recurring operation because it requires that you explicitly name each drawing in the script file itself. That means that before you can process a group of drawings, you first must edit the script file contents to specify file names. A few iterations of that process will have you looking for a better solution. Objects to the Rescue The solution we seek can be found using objects. In Visual LISP, objects present the opportunity to work with more than one drawing at a time. Now, in this case, the hitch is that the drawings need to be open and in a quiescent state -- that is, ready for a command to be run on each. But opening a set of drawings to be processed and then running a single Visual LISP macro is a lot easier than maintaining and updating a script file every time you need to do the layer renaming process. To use Visual LISP to reach out from the current drawing and into another open drawing, you must use the object access tools. Only through the object relationships can you clearly define which drawing and which object inside the drawing you are referencing. Naturally, there will be a few more lines of code involved -- just not as many as you might imagine. Object access in Visual LISP starts by including (vl-load-com) somewhere in your LISP code. This subr must be evaluated before you can access any of the VLAX functions in Visual LISP. Normally I put (vl-load-com) in my LISP files outside any (defun) function definitions so that it is evaluated when the LISP file is loaded. Once that subr is evaluated, the entire host of Visual LISP VLAX function are available, and repeated calls to (vl-load-com) are simply ignored. Now we are ready to start accessing objects. To begin, we need to gather all the open drawings. This is accomplished by getting the documents collection object inside the AutoCAD program object. The following expression results in a collection of the open documents: (setq dwgsObj A collection in Visual LISP terms is similar to a selection set or list. It is a group of similar objects, such as documents, layers, or blocks. You could also think of a collection as a special list of objects, much like a selection set is a special list of entity names. As such, there are several tools in Visual LISP specifically for processing collections. Looping through a collection, processing each member object one at a time, can be accomplished using (vlax-for). The (vlax-for) subr is a very simple sequential loop. Each member in a collection is placed in a local symbol for processing inside the loop. Consider the following Visual LISP code that uses the dwgsObj value established above:. (vlax-for dwg dwgsObj (print (vla-get-name dwg))) The above expression will loop through each open drawing and print the name of each drawing. The loop processes each drawing object in the dwgsObj collection, then prints the value of the name property in the drawing. Cool, huh? We can do a lot with the object for each drawing, such as accessing the layers collection and making modifications. So let's take a look at a function that works with a nested list of layer names (("old" "new")...) to change names, just like our earlier example. The difference now is that we want this version to work with every open drawing. (defun C:EXAMPLE3 (/ LYRS LYR) (setq LYRS '(("LAYER1" "L1") ("LAYER2" "L2") ("LAYER3" "L3")) dwgObj ) (vlax-for Dwg dwgObj (setq LYRObj (vla-get-layers DWG)) (foreach LYR LYRS (setq LD 'vla-item (list LYRObj (car LYR)))) (if (not LD)) (vla-put-name LD (cadr LYR))) ) ) ) The function employs a nested loop, with the outermost loop based on the open drawings collection. Inside the (vlax-for) loop, the layer object of open drawing is obtained and stored with LYRObj. Now the inner loop starts by iterating through each of the members of the LYRS list. The LYRS list contains the old and new names. We are to locate the old names, and when found, change them to the new. To read inside the layer collection object given the layer name, we use (vla-item). The problem is that if the layer name is not in the collection, we will get an automation error that terminates the function. To stop the error exit, we can use the Visual LISP error catch, as demonstrated in the code. is a subr that runs a function with parameters. When all goes well, we'll get the same result as when running the function directly. Should an error occur, the catch returns an error object instead of ending the evaluation. We test for the error object using the predicate subr This predicate results in "true" when the object is an error object. The predicate result is "nil" if the symbol does not point to an error object. In this case, the (vla-item) subr will result in either a layer object or an error object set in LD. Once we are certain that we have a layer object and not an error object, we can put the new name in place. The inner loop then iterates, going to the next layer name set. Once the layer list is complete, the drawings loop iterates by working with the next drawing. You can expand this example to do other manipulations with the objects in the open drawing. That is the joy of programming. Because we used objects to accomplish the task, the application can be scaled upward to run on a multitude of open drawings. That is the primary difference between the old school AutoLISP approach and the newer Visual LISP object-oriented approach. You can scale up object-related code much more easily. That's something to consider the next time you have to decide whether or not to use an object-oriented approach in your programming. Until next time, keep on programmin'. About the Author Bill Kramer is a Cadalyst contributing editor and author of the magazine's popular "Hot Tip Harry" column. He has been programming scientific and engineering solutions, writing magazine articles and books and teaching CAD/CAM/CAE customization for more than 30 years. Kramer wrote Cadalyst's first article about AutoLISP in 1986 before starting into the longest running AutoLISP series ever in the former Cadence magazine. =09 =09 =09 Harry Wants to Hear from You Do you have questions for Hot Tip Harry or feedback about Harry's Code Class article? Want to send a suggested topic for a future edition? E-mail your ideas to =09 Need Answers to Code Questions? Harry and friends are standing by online to aid AutoCAD users and programmers. Cadalyst's Hot Tip Harry-Help! forum offers assistance with AutoCAD programming and customization problems. Hot Tip Harry-Requests can help you locate a specific LISP or VBA routine for AutoCAD. Participation in Cadalyst Discussion Forums requires registration, but it's free. =09 What's New at Cadalyst.com Get the Code! Cadalyst's March code from Hot Tip Harry is available for download. Raymond Rizkallah earned this month's $100 Top Tip prize for Pipe and Column Extrusion, a set of 3D drawing utilities that simplifies the creation of pipes and columns by automating AutoCAD's Extrusion command based on an entity object selection. March "Hot Tip Harry" Live Online! Head over to Cadalyst.com for the latest edition of "Hot Tip Harry," as well as all your other favorite Cadalyst tutorials for AutoCAD and other popular CAD software, CAD Cartoon, Dialog Box (letters from readers) -- and all the content from the current edition of Cadalyst magazine. Get Your Weekly Fix of AutoCAD Tips Cadalyst's Tips & Tools Weekly e-article mails each Monday, packed with AutoCAD tips from readers -- and all reviewed by Cadalyst's volunteer Tip Patrol. You'll also receive a run-down of new online CAD resources, deals and freebies, the past week's new CAD-related hardware and software products, and more. View the archives and buy online today! Announcing Cadalyst Tips & Tricks Tuesdays Attention AutoCAD users! All it takes is 90 seconds and you could be off to more productive use of AutoCAD. Cadalyst announces Tips & Tricks Tuesdays with worldwide AutoCAD Technical Evangelist Lynn Allen. Each Tuesday, Lynn and Cadalyst bring a new, super-quick tip to the Cadalyst Video Gallery. Buy online to Tips & Tricks Tuesdays and receive an e-mail notification every time a new tip goes live in the Cadalyst Video Gallery. Benchmark 2008 Now Available The latest Cadalyst Systems Benchmark is now live on Cadalyst.com. Designed to test and compare systems running AutoCAD, C2008 is packed with new features including expanded 3D graphics tests and 2D/3D test options, a new Compare option at the end of the test, an improved Readme file with detailed instructions for using the test and evaluating results, and much more. =09 ACCOUNT MANAGEMENT Please do not reply to this e-mail message. Send your comments and questions directly to the Cadalyst editors at Important Note to Our Article Buy onliners: Cadalyst strongly urges you to add us to your Safe Senders list. Even if you have consistently received Cadalyst articles in the past, you should take this quick and easy step to ensure receipt going forward. Many e-mail service providers -- in particular Google Gmail and Yahoo -- are blocking mass mailings to their users, even e-mails users have requested. Policies and practices change from day-to-day; the only way to ensure that you receive all requested Cadalyst articles going forward is to add us to your Safe Senders list. Regardless of your service provider, please take a moment to do this today. To change your account preferences, follow the instructions below. Purchase: You are receiving MCAD Tech News at your request. You are buy onlined as: If you wish to leave this mailing list, simply purchase. Buy online: Sign up today for this or any of our free Cadalyst e-mail articles, including Tips & Tools Weekly, CAD Manager’s Article, AEC Tech News, MCAD Tech News, Tips & Tricks Tuesdays, GIS Tech News, What's New at Cadalyst, and Acrobat Insider. Please refer to our Privacy Policy. Cadalyst is a division of Questex Media Group, Inc., 201 Sandpointe Ave., Suite 500, Santa Ana, CA 92707. Copyright (c) 2008 Questex Media Group, Inc. Reproduction or distribution in whole or in part without written permission is prohibited.
... Back to listing 13
... Back to article 2468 | SITEMAP | Next to article 2470 ...
We would like to express our gratitude to all those who are kindly providing this unique content, especially to:
office@a3ides.com
olga@bemobile.md
tirlik@brainchildhunter.com
office@bemobile.md
sharuha@causeni.info
info@webconsulting.md
irina.gorea@bemobile.md
office@milieukontakt.nl
igor.romanyuk@bemobile.com.ua
yuri.sekretar@olympia.net.ua
info@tophost.md
webmoldova@yahoo.com
admin@bemobile.md
webmaster@bemobile.md
emil@a3ides.com
olea@bemobile.md
yuri.sekretar@bemobile.com.ua
contact@a3ides.com
info@a3ides.com
vitaliy.lozovskiy@bemobile.md
drugg83@yahoo.com
rector@usm.md
gaugash@usm.md
acad.rudic@mail.md
acadrudic@yahoo.com
veaceslav.vasilache@gmail.com
s.bakker@milieukontakt.nl
katea.melnicenco@gmail.com
mkoe.moldova@vox.md
musteata@doctor.com
renat.buga@ase.md
s.molenkamp@milieukontakt.nl
si_problemme@mail.ru
willem.tjebbe.oostenbrink@planet.nl
Tag cloud:
Cadalyst, AutoCAD, Helvetica,, drawing, sans-serif;, color:, command, font-size:, font-family:, line-height:, drawings, font-weight:, Cadalyst's, margin-top:, function, {font-size:, collection, #333333;, margin-bottom:, line-height:, font-family:, objects, AutoLISP, Discussion, #336699;, approach, drawings., running, normal;, routine,
Similar listing on:
Cadalyst, AutoCAD, Helvetica,, drawing, sans-serif;, color:, command, font-size:, font-family:, line-height:, drawings, font-weight:, Cadalyst's, margin-top:, function, {font-size:, collection, #333333;, margin-bottom:, line-height:, font-family:, objects, AutoLISP, Discussion, #336699;, approach, drawings., running, normal;, routine,
Similar listing on:
