Wednesday, February 24, 2010

SDI and MDI

SDI and MDI


 

When an application has the ability to open more than one document in a single session, this is known as multiple document interface. When developing an application for a MDI environment, special consideration must be taken, or the application may not function properly or it may interfere with other applications. To prevent applications from affecting one another, Autodesk has developed the concept of NameSpace. A NameSpace is a LISP environment that contains an isolated set of symbols (variables and functions). Each open drawing has it own unique NameSpace. Variables and functions defined in one NameSpace are isolated from variables and functions defined in another. When an AutoLISP application is loaded, the function is only accessible to the drawing where it was loaded. If the application is needed for multiple documents, then its contents can be automatically loaded by either appending the application to the Acaddoc.LSP or by using the VL-VLOAD-ALL function. When an AutoLISP application is compiled into a Visual LISP executable, the developer is given the choice of creating a separate NameSpace for the application. When the developer chooses this option, upon loading the application creates its own NameSpace separate from the NameSpace of the document where that the application was loaded. This guarantees that the variables used in the application will not be accidentally overwritten. Each time a compiled Visual LISP application is loaded in a different document, a new NameSpace is created to maintain the variables and functions associated with that application. When an application is compiled using the separate NameSpace option, by default that application's functions are not exposed to the document where the application is launched. The functions must be exported to the document using the VL-DOC-EXPORT function. To make an application accessible to the document and any other documents where the application is loaded, the VL-DOC-EXPORT function must be used. To determine which applications have defined separate NameSpaces and which of their functions have been exposed to the current document's NameSpace, the VL-LIST-LOADED-VLS and VL-LIST-EXPOSED-FUNCTIONS functions can be used. Once an application has been exposed using the VL-DOC-EXPORT function, its functions can be made available to other applications using the VL-DOC-EXPORT function. Variables contained within applications that define their own NameSpace are not known to the document's NameSpace where the application was loaded. Variables contained within a document's NameSpace may be accessed by a VLX application by using the VL-DOC-REF, VL-DOC-SET or VL-PROPAGATE functions.

The Value of a variable can be transferred from one document's NameSpace to another document's NameSpace using the Visual LISP Blackboard. A blackboard is a NameSpace that is separate from documents and VLX applications. Its purpose is to provide documents with a means of sharing data from one NameSpace to another.

Currently AutoLISP is limited to working with one document at a time. An AutoLISP application cannot issue any entity creation or modification functions in a NameSpace different from the one where the application is currently running.

Windows 95, 98 and NT Registry, Loading and Executing VBA applications

Windows 95, 98 and NT Registry, Loading and Executing VBA applications


 

Traditionally DOS and early versions of Windows (3.X) applications stored essential startup information in initialization (.INI) files. Initialization files are ASCII based text files that are limited in length to 64K. This created a problem; because these files were ASCII text files they were considered low security and could be edited using any text editor. It wasn't until the introduction of Windows NT that Microsoft began addressing these problems by developing a system of database files (collective known as the registry). These files are stored in a centralized location house important startup information used by Windows and other Windows based applications. The registry is a system of proprietary binary database files who's primary purpose is to ensure the integrity of the computer's operating system and applications. The registry uses a hierarchical structure to store information. The structure is comprised of six main components called subtrees (HKEY_LOCAL_MACHINE, HKEY_CLASS_ROOT, HKEY_CURRENT_USER, HKEY_USERS and HKEY_DYN_DATA). Each subtree is made up of subkeys. The subkeys contained in each subtree may also contain subkeys or they may contain active keys. It is the active keys that store the actual values associated with a particular application.

Out of the six subtrees located in the Windows registry, just two contain 90% of the data used by the registry (HKEY_LOCAL_MACHINE and HKEY_USER). The remaining subtrees (with the exception of the HKEY_DYN_DATA) are actually aliases that contain a copy of the contents of the other two keys. All information stored in the registry by an AutoLISP application should be done so in either the HKEY_LOCAL_MACHINE or HKEY_USER.

In addition to the Visual LISP IDE integrated into the AutoCAD environment, Autodesk has also provided the AutoCAD application developer with a second IDE (VBA) for developing applications. Although, AutoLISP is a powerful programming language for developing AutoCAD applications, VBA has the ability to create GUI interfaces much easier and faster than DCL. Because of Visual LISP's ability to incorporate other programming languages, the developer can create the GUI portion of an application with VBA and exchange data between VBA and AutoLISP components.

VL02.lsp

;;;********************************************************************

;;;

;;;    Program Name: VL02.lsp

;;;

;;;    Program Purpose: This program allows the user to calculate the ;;;                necessary bend allowances for soft steel. The

;;;                program uses formulas based on the formulas

;;;                found in the Machinery's Handbook 24th edition.

;;;    

;;;    Program Date: 12/31/98

;;;

;;;    Written By: James Kevin Standiford

;;;

;;;********************************************************************

;;;********************************************************************

;;;

;;;            Main Program

;;;

;;;********************************************************************

(defun c:bend (/ thickness radius_of_bend bend_angle allowance)

(setq    thickness (getreal "\nEnter Material Thickness : ")

    radius_of_bend (getreal "\nEnter Bend Radius : ")

    bend_angle (getreal "\nEnter Bend Angle : ")

)

(setq    allowance

     (* (+ (* 0.64 thickness) (* (* 0.5 3.14) radius_of_bend))

     (/ bend_angle 90)

     )

)

(princ (rtos allowance))

(princ)

)

(princ)

CAM.lsp

;;;********************************************************************

;;;

;;;    Program Name: CAM.lsp

;;;    Program Purpose: Create a Simple harmonic motion cam displacement

;;;             diagram and cam profile

;;;    Program Written By: James Kevin Standiford

;;;    Program Date : 01-31-99

;;;

;;;********************************************************************

;;;

;;;    Main Program

;;;

;;;********************************************************************

(defun c:cam (/     cir lo_l_c inc     up_r_c pt1a    pt2a pt1b

     pt2b pt1c pt2c pt1d     pt2d     pt1e    pt2e pt1f

     pt2f pt1g pt2g pt1h     pt2h     pt1i    pt2i pt1j

     pt2j pt1k pt2k pt1l     pt2l

     )

(setvar "cmdecho" 0)

(setq base (getreal "\nEnter base circle diameter : "))

(setq fo_heigh (getreal "\nEnter follower height : "))

(if (and (/= base nil) (/= fo_heigh nil))

(progn

(setq

    cir (* base pi)

    lo_l_c (getpoint "\nSelect lower left hand corner of graph : ")

    ;;

    ;;

    ;; Determines the outer edges of the grid, and calculates the vertical

    ;; lines of the grid. The variable lo_l_c is the lower left-hand corner

    ;; of the graph. The variable up_r_c is the upper right hand corner of

    ;; the graph. The variables pt1? represent the lower portion of the vertical

    ;; grid line. The variables pt2? represent the upper portion of the vertical

    ;; grid line.

    ;;

    ;;

    inc (/ cir 12)

    up_r_c lo_l_c

    up_r_c (subst (+ cir (car lo_l_c))

         (car lo_l_c)

         (subst (+ fo_heigh (cadr lo_l_c))

             (car (cdr lo_l_c))

             lo_l_c

         )

     )

    pt1a (list (+ inc (car lo_l_c)) (cadr lo_l_c))

    pt2a (list (car pt1a) (cadr up_r_c))

    pt1b (list (+ (* 2 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2b (list (car pt1b) (cadr up_r_c))

    pt1c (list (+ (* 3 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2c (list (car pt1c) (cadr up_r_c))

    pt1d (list (+ (* 4 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2d (list (car pt1d) (cadr up_r_c))

    pt1e (list (+ (* 5 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2e (list (car pt1e) (cadr up_r_c))

    pt1f (list (+ inc (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2f (list (car pt1f) (cadr up_r_c))

    pt1g (list (+ (* 6 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2g (list (car pt1g) (cadr up_r_c))

    pt1h (list (+ (* 7 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2h (list (car pt1h) (cadr up_r_c))

    pt1i (list (+ (* 8 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2i (list (car pt1i) (cadr up_r_c))

    pt1j (list (+ (* 9 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2j (list (car pt1j) (cadr up_r_c))

    pt1k (list (+ (* 10 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2k (list (car pt1k) (cadr up_r_c))

    pt1l (list (+ (* 11 inc) (car lo_l_c))

         (car (cdr lo_l_c))

     )

    pt2l (list (car pt1l) (cadr up_r_c))

)

;;

;;

;; Constructs a rectangle thatrepresents the outline of the grid and

;; all vertical grid lines.

;;

;;

(command "line"

     lo_l_c

     (subst (car up_r_c) (car lo_l_c) lo_l_c)

     up_r_c

     (subst (cadr up_r_c) (cadr lo_l_c) lo_l_c)

     "c"

     "line"

     pt1a

     pt2a

     ""

     "line"

     pt1b

     pt2b

     ""

     "line"

     pt1c

     pt2c

     ""

     "line"

     pt1d

     pt2d

     ""

     "line"

     pt1e

     pt2e

     ""

     "line"

     pt1g

     pt2g

     ""

     "line"

     pt1h

     pt2h

     ""

     "line"

     pt1i

     pt2i

     ""

     "line"

     pt1j

     pt2j

     ""

     "line"

     pt1k

     pt2k

     ""

     "line"

     pt1l

     pt2l

     ""

)

;;

;;

;; Calculates the horizontal grid lines and the intersections of

;; of the cam displacement graph with the grid, thus establishing the

;; location of the displacement graph.

;;

;;

(setq first_y_di

            (abs (-    (+ (* (cos (/ (* 30 pi) 180)) (/ fo_heigh 2))

                 (/ fo_heigh 2)

                )

                fo_heigh

             )

            )

     second_y_di

            (abs (-    (+ (* (cos (/ (* 60 pi) 180)) (/ fo_heigh 2))

                 (/ fo_heigh 2)

                )

                fo_heigh

             )

            )

     third_y_di    (/ fo_heigh 2)

     fourth_y_di

            (abs (-    (+ (* (cos (/ (* 120 pi) 180)) (/ fo_heigh 2))

                 (/ fo_heigh 2)

                )

                fo_heigh

             )

            )

     fifth_y_di

            (abs (-    (+ (* (cos (/ (* 150 pi) 180)) (/ fo_heigh 2))

                 (/ fo_heigh 2)

                )

                fo_heigh

             )

            )

     pt3a    (list (car lo_l_c)

             (setq fya (+ first_y_di (cadr lo_l_c)))

            )

     pt4a    (list (car up_r_c) fya)

     pt3b    (list (car lo_l_c)

             (setq fyb (+ second_y_di (cadr lo_l_c)))

            )

     pt4b    (list (car up_r_c) fyb)

     pt3c    (list (car lo_l_c)

             (setq fyc (+ third_y_di (cadr lo_l_c)))

            )

     pt4c    (list (car up_r_c) fyc)

     pt3d    (list (car lo_l_c)

             (setq fyd (+ fourth_y_di (cadr lo_l_c)))

            )

     pt4d    (list (car up_r_c) fyd)

     pt3e    (list (car lo_l_c)

             (setq fye (+ fifth_y_di (cadr lo_l_c)))

            )

     pt4e    (list (car up_r_c) fye)

)

;;

;;

;; Draws the horizontal lines and displacement diagram.

;;

;;

(command "line"

     pt3a

     pt4a

     ""

     "line"

     pt3b

     pt4b

     ""

     "line"

     pt3c

     pt4c

     ""

     "line"

     pt3d

     pt4d

     ""

     "line"

     pt3e

     pt4e

     ""

     "spline"

     lo_l_c

     (list (car pt1a) (cadr pt3a))

     (list (car pt1b) (cadr pt3b))

     (list (car pt1c) (cadr pt3c))

     (list (car pt1d) (cadr pt3d))

     (list (car pt1e) (cadr pt3e))

     (list (car pt1g) (cadr up_r_c))

     (list (car pt1h) (cadr pt3e))

     (list (car pt1i) (cadr pt3d))

     (list (car pt1j) (cadr pt3c))

     (list (car pt1k) (cadr pt3b))

     (list (car pt1l) (cadr pt3a))

     (list (car up_r_c) (cadr lo_l_c))

     ""

     ""

     ""

)

;;

;;

;; Prompts the user to select the location of the CAM profile.

;; Calculates the point defining the CAM profile. Draws the CAM

;; Profile.

;;

;;

(setq basepoint (getpoint "\nSelect location for cam profile : "))

(command

    "spline"

    (list (car basepoint) (+ (cadr basepoint) base))

    (list (- (car basepoint) (* (+ first_y_di base 0.05) 0.5))

     (+ (cadr basepoint)

         (* (+ first_y_di base 0.05) 0.866025403)

     )

    )

    (list (- (car basepoint)

         (* (+ second_y_di base 0.05) 0.866025403)

     )

     (+ (cadr basepoint) (* (+ second_y_di base 0.05) 0.5))

    )

    (list (- (car basepoint) (* (+ third_y_di base 0.05) 1))

     (+ (cadr basepoint) (* (+ third_y_di base 0.05) 0))

    )

    (list (- (car basepoint)

         (* (+ fourth_y_di base 0.05) 0.866025403)

     )

     (- (cadr basepoint) (* (+ fourth_y_di base 0.05) 0.5))

    )

    (list (- (car basepoint) (* (+ fifth_y_di base 0.05) 0.5))

     (- (cadr basepoint)

         (* (+ fifth_y_di base 0.05) 0.866025403)

     )

    )

    (list (car basepoint)

     (- (cadr basepoint) (+ fo_heigh base 0.05))

    )

    (list (+ (car basepoint) (* (+ fifth_y_di base 0.05) 0.5))

     (- (cadr basepoint)

         (* (+ fifth_y_di base 0.05) 0.866025403)

     )

    )

    (list (+ (car basepoint)

         (* (+ fourth_y_di base 0.05) 0.866025403)

     )

     (- (cadr basepoint) (* (+ fourth_y_di base 0.05) 0.5))

    )

    (list (+ (car basepoint) (* (+ third_y_di base 0.05) 1))

     (+ (cadr basepoint) (* (+ third_y_di base 0.05) 0))

    )

    (list (+ (car basepoint)

         (* (+ second_y_di base 0.05) 0.866025403)

     )

     (+ (cadr basepoint) (* (+ second_y_di base 0.05) 0.5))

    )

    (list (+ (car basepoint) (* (+ first_y_di base 0.05) 0.5))

     (+ (cadr basepoint)

         (* (+ first_y_di base 0.05) 0.866025403)

     )

    )

    (list (car basepoint) (+ (cadr basepoint) base))

    ""

    ""

    ""

    "circle"

    basepoint

    base

)

)

(princ "\nInsufficient Data Press enter to try again : ")

)

)

(princ "\nTo excute enter cam at the command prompt ")

(princ)

Resistance.lsp

;;;********************************************************************

;;;    Program Name: Resistance.lsp

;;;    Program Purpose: Calculate the voltage drop across two resistors

;;;            in a series, as well as the current for the cirrcuit.

;;;    Date: 1/13/99

;;;    Programmed By: James Kevin Standiford

;;;

;;;********************************************************************

;;;

;;;    Main Program

;;;

;;;********************************************************************

(DEFUN C:resistance (/         voltage     resistance_1

         resistance_2 equivalent     value_1

         value_2     current     name

         length_string name_truncated file_name

         )

;;;

;;; Obtain Information and Perform Calculations

;;;

(SETQ

file_name     (STRCAT (SUBSTR (GETVAR "dwgname")

                 1

                 (- (STRLEN (GETVAR "dwgname")) 4)

             )

             ".RLT"

         )

file     (open file_name "a")

voltage     (GETREAL "\nEnter voltage of circuit : ")

resistance_1 (GETREAL "\nEnter resistance of resistor #1 : ")

resistance_2 (GETREAL "\nEnter resistance of resistor #2 : ")

equivalent     (+ resistance_1 resistance_2)

current     (/ voltage equivalent)

value_1     (* current resistance_1)

value_2     (* current resistance_2)

)

;;;

;;; Print Information to Both a File and Screen

;;;

(PRINC

(PRINC (STRCAT "\nThe current drawn by this circuit is "

         (RTOS current)

     )

     file

)

)

(PRINC

(PRINC (STRCAT "\nThe equivalent resistance for this circuit is "

         (RTOS equivalent)

     )

     file

)

)

(PRINC

(PRINC (STRCAT "\nThe voltage drop across resistor #1 is "

         (RTOS value_1)

     )

     file

)

)

(PRINC

(PRINC (STRCAT "\nThe voltage drop across resistor #2 is "

         (RTOS value_2)

     )

     file

)

)

(close file)

(PRINC)

)

(princ)

(princ "\nEnter Resistance to start program ")

(princ)

Sunday, February 21, 2010

Modifying Extended Entity Data

Once the extended entity data has been retrieved using the ENTGET function, the programmer is free to modify any portion or all of the information using the list and association list functions previously discussed. At this point the extended entity data is treated just as the association list data was in Chapter Four. The dotted pair containing the old data is first obtained using the ASSOC function. Next, the new dotted pair is created using the CONS function and the two are switched using the SUBST function. Finally, the changes are recorded in the drawing database using the ENTMOD function. Using the extended entity data from the previous example the following expressions would be used to replace the dotted pair (1041 . 14.5) with the dotted pair (1041 . 0.0125).

Retrieving Extended Entity Data from an AutoCAD Object

When an object has been assigned extended entity data it cannot be accessed using traditional AutoCAD object listing or entity modifying commands (LIST, DDMODIFY, PROPERTIES, etc.). This alone ensures the developer that the information assigned to the object can not be tampered with by the user. The only way the information can be extracted once it has been assigned is with the use of the ENTGET function. Recall from Chapter Four that when this function is supplied with any entity's name it returns the definition data for the specified object. However, after a closer examination of the function's syntax one important aspect emerges. This function, when supplied with an application name in addition to the entity name, returns the entity definition along with the extended entity data that is associated with the specified application. For example, to obtain the extended entity data that was assigned to an object by the previous program, the following syntax would be used.

Command: (SETQ entity_data (ENTGET (CAR (ENTSEL)) '("EXAMPLE_1_EXTENDED_DATA"))) ~EnterKey~

Select object:

((-1 . <Entity name: 2770500>) (0 . "LINE") (5 . "20") (100 .

"AcDbEntity") (67 . 0) (8 . "0") (100 . "AcDbLine") (10 2.19416 5.16515 0.0) (11 5.15258 2.37318 0.0) (210 0.0 0.0 1.0) (-3 ("EXAMPLE_1_EXTENDED_DATA" (1000 . "Imagine what can be saved to extended entity data") (1041 . 4.5) (1042 . 10.0))))

Command:

In order to retrieve the xdata assigned to each application associated with an object, all applications names must be supplied to the ENTGET function. To retrieve the extended entity data appended to an object's definition data by the application names applications EXAMPLE_1_EXTENDED_DATA and EXAMPLE_2_EXTENDED_DATA, the following syntax would be used.