My Photo

Search


  • Search 280+ postings

Site Awards



Syndications for this Site


Out with Some More Old and In with Some More New

As I continue to purge out a lot of my older formatted web pages and roll in the new look and design, I keep an eye on ways to better streamline the content that I develop and deliver through my website.  One of the newest sections of the site to get a face lift is the AutoLISP functions listing, while it is not fully converted over yet, the information under the section is much easier to view and access now.

The information on the pages will be morphing in the future to include references to sample code that I have on my site for download.  By linking the two resources together, it will help users that are new to AutoLISP and want to learn a little more about the functions that are in a routine that I offer, and for those of you that are looking to enhance your existing programs with some new code. Let me know if you have any ideas that would help to improve the site and the content further.

Click here to access the new AutoLISP Functions area on HyperPics.com.

Sincerely,
  Lee

Working with Environment Variables through AutoLISP

Ok I know environment variables are not as popular as they once use to be, but they can be very powerful in setting up unique values on the machine for company settings.  As you might have noticed in a few of the past postings I have been releasing sample code that uses the FSO (File System Object) of the Shell object.

This sample follows that same trend and uses the Shell object to manipulate environment variables that are maintained by the OS.  Below are three, yes I said three different examples of using the Shell object.  The first shows how to expand environment variables in a string.  This can be great for building file paths or getting information about the OS or PC.

;; Shows how to use expanding environment strings
;; Usage: (ExpEnvStr "%TEMP%\\MYDATA")
;; Results of sample: "C:\\DOCUME~1\\Lee\\LOCALS~1\\Temp\\MYDATA"
(defun ExpEnvStr ( strVal / wshShell)
  (vl-load-com)

  (setq wshShell (vlax-create-object "WScript.Shell"))
  (vlax-invoke-method wshShell 'ExpandEnvironmentStrings strVal)
)

;; Retreive the value of the environment variable
;; Usage: (GetEnvStr "SYSTEM" "USERID")
(defun GetEnvStr ( strVarType strVarName / wshShell envVars)
  (vl-load-com)

  (setq wshShell (vlax-create-object "WScript.Shell"))
  (setq envVars (vlax-get-property wshShell 'Environment strVarType))
  (vlax-get-property envVars 'Item strVarName)
)

;; Set the value to an environment variable
;; Usage: (SetEnvStr "SYSTEM" "USERID" "L123")
(defun SetEnvStr ( strVarType strVarName strVarVal / wshShell envVars)
  (vl-load-com)

  (setq wshShell (vlax-create-object "WScript.Shell"))
  (setq envVars (vlax-get-property wshShell 'Environment strVarType))
  (vlax-put-property envVars 'Item strVarName strVarVal)
)

Got an idea for some sample code?  If you do, I would like to hear from you.  Your idea might be selected and the results posted on the blog or website.

Sincerely,
  Lee

Creating a New Document in AutoCAD with AutoLISP

It must be getting close to the end of the year and time to clean out the good old sample vault.  Many of the samples that have been put up on my blog have been created throughout this year based on questions that I have gotten through e-mail, in passing at events such as AU and even from my online forum.

Have you ever wanted to create a new document using a macro and tried to use the NEW command to do this?  Using the NEW command might not be the best approach.  Below is a very basic custom function that demonstrates how to create a new drawing based on a template through AutoLISP.

;; Begin Code
;; Written by: Lee Ambrosius
;; Command macro option ^P(createnewdwg "acadiso.dwt")

;; Load the Visual LISP environment
(vl-load-com)

;; Custom routine that allows you to create a new drawing
(defun createNewDWG (templateName)
  (vla-activate (vla-add (vla-get-documents (vlax-get-acad-object)) templateName))
(princ)
)
;; End Code

Sincerely,
  Lee

Printing a MS Word Doc using AutoLISP

Its that time of the year for giving out gifts and such... so today I have decided to drop a smaple of code that deomstrates how to print a MS Word document using AutoLISP.  What a great way to print your holiday newsletter while the boss things you are working in AutoCAD.  OK maybe not thebest of uses for the code sample.  It is possible to automate the printing process of not only your AutoCAD drawings, but also the spces that might need to go out with them at the same time.  The code can be modified to specifiy a printer if need be, but it is designed to go to the default printer though.

:: Begin Code
;; Written by: Lee Ambrosius, HyperPics LLC

;; Usage: (PrintMSWordDoc "C:\\test.doc")
;; Open a document in MS Word and Print it out
(defun PrintMSWordDoc ( strWordDoc / wordObj wordDocsObj wordDocObj)
  (vl-load-com)

  ;; Variable to define constant used in the Close and Quit methods
  (setq wdDoNotSaveChanges 0)

  ;; Create a new MS Word object
  (setq wordObj (vlax-invoke-method (vlax-get-acad-object) 'GetInterfaceObject "Word.Application"))

  ;; Get a reference to the Docuemnts collection in Word
  (setq wordDocsObj (vlax-get-property wordObj 'Documents))

  ;; Open the drawing
  (setq wordDocObj (vlax-invoke-method wordDocsObj 'Open strWordDoc))

  ;; Print it out
  (vlax-invoke-method wordDocObj 'PrintOut)

  ;; Close the document and don't save any changes
  (vlax-invoke-method wordDocObj 'Close wdDoNotSaveChanges)

  ;; Close the instance of the MS Word application
  (vlax-invoke-method wordObj 'Quit wdDoNotSaveChanges)
(princ)
)
;; End Code

Sincerely,
  Lee

Which Drives are Available?

From time to time you might want your AutoLISP application to be able to write to a specific drive or drives in a specific order if you are working with a group of users that might do some travelling.  If you want to test to see which drives are available through AutoLISP, you could use the function vl-directory-files.  This would force you to test each letter combination which can take time and it doesn't tell you if the drive is ready or not.  Below is an example using the FSO object to get all the available drives on the machine, and test to see what type of drive it is and if it is ready or not.

;; Begin Code
(defun GetDrives ( / wshFSO driveObjs)
  (vl-load-com)

  (if (= wshLibImport nil)
    (progn
      (vlax-import-type-library :tlb-filename "c:\\windows\\system32\\wshom.ocx"
                          :methods-prefix "wshm-"
                          :properties-prefix "wshp-"
                          :constants-prefix "wshk-"
      )
      (setq wshLibImport T)
    )
  )

  (setq wshFSO (vlax-create-object "Scripting.FileSystemObject"))
  (setq driveObjs (wshp-get-Drives wshFSO))

  (vlax-for driveObj driveObjs
    (progn
      (prompt (strcat "\nDrive Letter: " (vlax-get-property driveObj 'DriveLetter)))
      (prompt "\nDrive Type: ")
      (cond
        ((= (vlax-get-property driveObj 'DriveLetter) wshk-CDRom)(prompt "CDROM"))
        ((= (vlax-get-property driveObj 'DriveLetter) wshk-RamDisk)(prompt "Ram Disk"))
        ((= (vlax-get-property driveObj 'DriveLetter) wshk-Fixed)(prompt "Fixed"))
        ((= (vlax-get-property driveObj 'DriveLetter) wshk-Removable)(prompt "Removeable"))
        ((= (vlax-get-property driveObj 'DriveLetter) wshk-UnknownType)(prompt "Unknown"))
      )
      (prompt (strcat "\nIs Ready: "))
      (if (= (vlax-get-property driveObj 'IsReady) :vlax-true)
        (prompt "True")
        (prompt "False")
      )
    )
  )
(princ)
)
;; End Code

Sincerely,
  Lee

Obtaining a Short File Name through LISP

Many that know me, know that I like to dabble from time to time with programming AutoCAD.  Ok, maybe it is not just dabbling, but rather pushing things right to the edge.  If you have an old program or have a need for file name structure of 8.3 then you might enjoy the routine below.  It is an example of how AutoLISP or Visual LISP can use COM objects to extend the functionality of the language.  The sample shows how to use the File System Object.

;; Begin Code
(defun GetShortName ( strFileName / wshFSO fileObj)
  (vl-load-com)

  (setq wshFSO (vlax-create-object "Scripting.FileSystemObject"))
  (setq fileObj (vlax-invoke-method wshFSO 'GetFile strFileName))
  (vlax-get-property fileObj 'ShortPath)
)
;; End Code

Happy coding...

Sincerely,
  Lee

System Variables Got You Stumped

Have you ever been in the situation where something in one drawing just doesn't seem to be working correctly, but it works in a different drawing.  There are many different System Variables in AutoCAD these days.  System Variables play a vital role in some of the settings that can affect commands in a drawing.  Most System Variables are stored in the drawing or in the Windows Registry, with some that don't get saved at all.  I have been using a utility that dates back to 1998 that I wrote to help manage my System Variables if they get out of line or even when I am working with client drawings.

I have just recently posted an updated copy that now works with release starting back with R12 up through 2006.  You can download the latest version by clicking here.

Check out some of the other available downloads by clicking here.

Looking for some information on System Variables in AutoCAD? Check out the System Variable matrix that I maintain on my website by clicking here.

Sincerely,
  Lee

AutoCAD 2006 - Lock Attribute Position

Attlockposition_1 AutoCAD 2006 introduces many great enhancements with blocks and the way they are inserted and managed.  One of the new features that is part of the updates in AutoCAD 2006 is the ability to set an Attribute so its position is locked and therefore can't be moved using grip editing.  The property can be changed through the Properties palette after it has been created through the Attribute Definition dialog box or ATTDEF command.  But what if you wanted to update an existing attribute through code or add this functionality to an existing LISP routine that you might have in your tool kit.

I have taken the time to figure out who Autodesk is handling this functionality at the object level.  Just like the Dimension Mask (or Fill Color) in AutoCAD 2005/2006, they are using Xdata on the object.  Below is a sample set of code that demonstrates how to set and unset the Lock Position behavior on an Attribute Definition.

;; Begin code
;; Written by Lee Ambrosius (lee_ambrosius@hyperpics.com)
;;                 HyperPics LLC (http://www.hyperpics.com)

;; Created on: 5/12/2005

;; AutoCAD Release: 2006

;; This program allows you to lock the position of an Attribute.

;; Sets the Lock Position behavior
(defun c:SetAttLock()
  (setLockPosition
    (car (entsel "\nSelect Attribute Definition to set Lock Position: "))
    '("AcDbAttr" (1070 . 0) (1070 . 1))
  )
)

;; Removes the Lock Position behavior
(defun c:RemoveAttLock()
  (setLockPosition
    (car (entsel "\nSelect Attribute Definition to remove Lock Position: "))
    '("AcDbAttr" (1070 . 0) (1070 . 0))
  )
)

;; Example (setLockPosition entityName '("AcDbAttr" (1070 . 0) (1070 . 1))))
(defun setLockPosition (entityName lstValue / entData)
  (if (/= entityName nil)
    (progn
      (setq entData (entget entityName '("AcDbAttr")))

      (if (= (cdr (assoc 0 entData)) "ATTDEF")
        (progn
          (if (/= (assoc -3 entData) nil)
            (if (= (nth 0 (cadr (assoc -3 entData))) "AcDbAttr")
              (setq entData (subst (cons -3 (list lstValue)) (assoc -3 entData) entData))
              (setq entData (append entData (list (list -3 lstValue))))
            )
            (setq entData (append entData (list (list -3 lstValue))))
          )

          (entmod entData)
        )
      )
    )
  )
(princ)
)
;; End code

Happy coding...

Sincerely,
  Lee

AutoCAD Customization/ProgrammingTraining

I am proud to announce that  I have partnered up with Virtual Partners to offer AutoCAD training in the areas of both General and Customization/Programming.  The schedule will change with demands on classes, but we have slotted classes from January through February.  These classes range from customizing AutoCAD, using AutoLISP and VBA to great automation and improving work flow to general AutoCAD upgrade classes.  If you have any questions please contact James Murphy at murph@virtualpartners.com or myself at lee_ambrosius@hyperpics.com.

Class schedule and information can be found on the Virtual Partners website.  Contact us today to learn how to unlock the secrets and benefits of customizing AutoCAD; education is a great tool so start the New Year off on the right foot.

Sincerely,
  Lee

Layer Filter Removal Tool

Based on a recent request to have a command that removes all the Layer Filters from a drawing; I decided to dig through all my samples on my machine and post it.  The tool is written in Visual LISP and is now on my website.  It has two defined commands to run the tool once it is loaded.  The long named command is RemoveLayerFilters and the short name is RLF.  Hope you find it helpful.

Click here to download the Remove Layer Filters tool
Click here to see other AutoLISP tools that are available for download

Sincerely,
  Lee

Feeds


  • Between the Lines Blog

  • Will render for food... Digital Media Blog

  • HyperPics.com