Author Topic: Create command to draw an object..?  (Read 3929 times)

0 Members and 1 Guest are viewing this topic.

CECE_CAD

  • Guest
Create command to draw an object..?
« on: May 28, 2008, 04:26:33 PM »
I would like to have a command create the drawing attached..  I have no idea how to even start this..  I would like the user to be prompted to enter the “Round” width, then the “Square” width, and then the depth.  I would like to learn how to create this routine as it’s over my head.  If someone can help assist me I’m willing to dig in. 

daron

  • Guest
Re: Create command to draw an object..?
« Reply #1 on: May 28, 2008, 04:48:12 PM »
And start you will.
First you need the user to supply:
point1 and point 2, then a height.
(setq pt1 (getpoint "\nSelect first point: "))
(setq pt2 (getpoint pt1 "\nSelect width: "))
etc.
Then ask the user to supply an angle or length of top line.
Take information, throw in some trig and the square part (name escapes me) is ready.
For the triangle you already have point1 and 2. To get the midpoint, use polar.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Create command to draw an object..?
« Reply #2 on: May 28, 2008, 04:50:37 PM »
I don't have time this evening to help you with the code but I can tell you the first step is creating some pseudo code.
This is a description of what you want to accomplish in the order that logically works through the task.

In your case you started out with some descriptions already.

Code: [Select]
User input: get the Square width
User input: get the Round width (diameter)
User input: get the length or run of the transition
[Note: these were all distances so use the getdist function and you
may want to use the initget to filter out incorrect choices]
User input: get the location to draw the object

Error check all user input, stop if incorrect
Verify and / or create the DUCT layer
Draw the object using COMMAND or entmakex
User input: get rotation of the new object

Does that describe the task?

<edit: added rotation of the object>

I see you are in good hands. See you all tomorrow.
 :-)
« Last Edit: May 28, 2008, 11:58:05 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CECE_CAD

  • Guest
Re: Create command to draw an object..?
« Reply #3 on: May 28, 2008, 05:07:09 PM »
Cab, Wow..  :-D   The "Round" is the name of that end of the block.. It’s not really a diameter, yes you definitely described the task.. :lol:

« Last Edit: May 28, 2008, 05:13:58 PM by CECE_CAD »

quamper

  • Guest
Re: Create command to draw an object..?
« Reply #4 on: May 28, 2008, 05:15:21 PM »
How about a dynamic block? :D

I assume you probably have certain "valid" sizes. You could add a lookup to the block where you could pick from a drop down list choices. Or if nothing else the distance on the various sides could be restricted to "sane" values. (you can also set the 3 values via the properties window)

Not what you were looking for per se. Especially if you're using a version of autocad that doesn't support dyn blocks! :D

CECE_CAD

  • Guest
Re: Create command to draw an object..?
« Reply #5 on: May 28, 2008, 05:19:03 PM »
Quamper,  I was thinking of creating a dynamic block but my users thought they would like this better..  I like the block, I will test both out with them.. Also, this is a good lesson for me to learn..  Thanks for the suggestion..

How about a dynamic block? :D

I assume you probably have certain "valid" sizes. You could add a lookup to the block where you could pick from a drop down list choices. Or if nothing else the distance on the various sides could be restricted to "sane" values. (you can also set the 3 values via the properties window)

Not what you were looking for per se. Especially if you're using a version of autocad that doesn't support dyn blocks! :D

CECE_CAD

  • Guest
Re: Create command to draw an object..?
« Reply #6 on: May 28, 2008, 05:20:52 PM »
Daron,  Do I need to set more pnts like this,  (/ pnt1 pnt2 pnt3 pnt4)?  Or do I not need that?

And start you will.
First you need the user to supply:
point1 and point 2, then a height.
(setq pt1 (getpoint "\nSelect first point: "))
(setq pt2 (getpoint pt1 "\nSelect width: "))
etc.
Then ask the user to supply an angle or length of top line.
Take information, throw in some trig and the square part (name escapes me) is ready.
For the triangle you already have point1 and 2. To get the midpoint, use polar.

daron

  • Guest
Re: Create command to draw an object..?
« Reply #7 on: May 28, 2008, 10:53:31 PM »
You might. It depends on how much will be needed by the program. I was just trying to get you started. Open the vlide and start typing and testing your code to see what it does. You can also access the help files easily from there.

CECE_CAD

  • Guest
Re: Create command to draw an object..?
« Reply #8 on: June 04, 2008, 04:22:20 PM »
Ok, I'm not sure if i'm understanding..   :?  I'm not getting it to draw, I know that I don 't have enough points but I'm just trying to see if I could start it.  I know this looks bad :|

Code: [Select]
(Defun C:SQR (); Draw Polyline [rectangle]
 (command "layer" "M" "Equip" "C" "123" "Equip")
 (setq oecho (getvar "cmdecho"))
 (setq pt1 (getpoint "\nSelect first point: "))
 (setq pt2 (getpoint pt1 "\nSelect Round width: "))
 (setq pt3 (getpoint pt2 "\nSelect depth: "))
 (command "pline" pt1 pt2 "")
 (setvar "cmdecho" oecho)
(princ)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Create command to draw an object..?
« Reply #9 on: June 05, 2008, 12:23:14 AM »
CECE
Here is a very basic code to get you started based on the pseudo code I posted.
There is no error checking & needs improvements.
Code: [Select]
(Defun C:SQR (/ SquareWidth RoundWidth Run ptBase ll lr ur ul um ent1 ent2)
  (command "layer" "M" "Equip" "C" "123" "Equip" "")
  (if
    (and
      (setq SquareWidth (getdist "\nEnter the Square Width: "))
      (setq RoundWidth (getdist "\nEnter the Round Width: "))
      (setq Run (getdist "\nEnter the lenght: "))
    )
     (progn
       ;;  draw the object at 0,0 as the base point at middle of
       ;;  the square end, then move to desired location.
       (setvar "plinewid" 0)
       (setq ptBase '(0 0)
             ll     (polar ptBase pi (/ SquareWidth 2.))
             lr     (polar ptBase 0.0 (/ SquareWidth 2.))
             um     (polar ptBase (/ pi 2) Run)
             ul     (polar um pi (/ RoundWidth 2.))
             ur     (polar um 0.0 (/ RoundWidth 2.))
       )
       (command "._pline" "_non" ll "_non" lr "_non" ur "_non" ul "_close")
       (setq ent1 (entlast))
       (command "._pline" "_non" ll "_non" um "_non" lr "")
       (setq ent2 (entlast))
       (command "_.change" ent2 "" "P" "C" "White" "")
       (command "_.move" ent1 ent2 "" "_non" ptBase pause)
     )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Create command to draw an object..?
« Reply #10 on: June 05, 2008, 10:07:57 AM »
Check out the following:

;;; PVSYMBOLS.LSP   a program to insert in-line pipe valve
;;; symbols with or without trimming the line.  Program by Tony Hotchkiss.
;;; 8-2-02, Revised local variables in (init) function

Gary
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

GDF

  • Water Moccasin
  • Posts: 2081
Re: Create command to draw an object..?
« Reply #11 on: June 05, 2008, 10:15:56 AM »
CECE
Here is a very basic code to get you started based on the pseudo code I posted.
There is no error checking & needs improvements.
Code: [Select]
(Defun C:SQR (/ SquareWidth RoundWidth Run ptBase ll lr ur ul um ent1 ent2)
  (command "layer" "M" "Equip" "C" "123" "Equip" "")
  (if
    (and
      (setq SquareWidth (getdist "\nEnter the Square Width: "))
      (setq RoundWidth (getdist "\nEnter the Round Width: "))
      (setq Run (getdist "\nEnter the lenght: "))
    )
     (progn
       ;;  draw the object at 0,0 as the base point at middle of
       ;;  the square end, then move to desired location.
       (setvar "plinewid" 0)
       (setq ptBase '(0 0)
             ll     (polar ptBase pi (/ SquareWidth 2.))
             lr     (polar ptBase 0.0 (/ SquareWidth 2.))
             um     (polar ptBase (/ pi 2) Run)
             ul     (polar um pi (/ RoundWidth 2.))
             ur     (polar um 0.0 (/ RoundWidth 2.))
       )
       (command "._pline" "_non" ll "_non" lr "_non" ur "_non" ul "_close")
       (setq ent1 (entlast))
       (command "._pline" "_non" ll "_non" um "_non" lr "")
       (setq ent2 (entlast))
       (command "_.change" ent2 "" "P" "C" "White" "")
       (command "_.move" ent1 ent2 "" "_non" ptBase pause)
     )
  )
  (princ)
)

Alan

I can use this, thanks for the heads up.

Gary
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

fixo

  • Guest
Re: Create command to draw an object..?
« Reply #12 on: June 05, 2008, 12:19:18 PM »
CECE
Here is a very basic code to get you started based on the pseudo code I posted.
There is no error checking & needs improvements.
Code: [Select]
(Defun C:SQR (/ SquareWidth RoundWidth Run ptBase ll lr ur ul um ent1 ent2)
  (command "layer" "M" "Equip" "C" "123" "Equip" "")
  (if
    (and
      (setq SquareWidth (getdist "\nEnter the Square Width: "))
      (setq RoundWidth (getdist "\nEnter the Round Width: "))
      (setq Run (getdist "\nEnter the lenght: "))
    )
     (progn
       ;;  draw the object at 0,0 as the base point at middle of
       ;;  the square end, then move to desired location.
       (setvar "plinewid" 0)
       (setq ptBase '(0 0)
             ll     (polar ptBase pi (/ SquareWidth 2.))
             lr     (polar ptBase 0.0 (/ SquareWidth 2.))
             um     (polar ptBase (/ pi 2) Run)
             ul     (polar um pi (/ RoundWidth 2.))
             ur     (polar um 0.0 (/ RoundWidth 2.))
       )
       (command "._pline" "_non" ll "_non" lr "_non" ur "_non" ul "_close")
       (setq ent1 (entlast))
       (command "._pline" "_non" ll "_non" um "_non" lr "")
       (setq ent2 (entlast))
       (command "_.change" ent2 "" "P" "C" "White" "")
       (command "_.move" ent1 ent2 "" "_non" ptBase pause)
     )
  )
  (princ)
)

Alan, it's cool
+100%

~'J'~

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Create command to draw an object..?
« Reply #13 on: June 05, 2008, 12:21:30 PM »
Thank you Sirs.  8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CECE_CAD

  • Guest
Re: Create command to draw an object..?
« Reply #14 on: June 05, 2008, 01:24:13 PM »
CAB,

Thanks.. That works great!  I'm just trying to understand how you did that.. 

Code: [Select]
(Defun C:SQRR (/ SquareWidth RoundWidth Run ptBase ll lr ur ul um ent1 ent2) 
those are all the variables? 

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Create command to draw an object..?
« Reply #15 on: June 05, 2008, 01:31:57 PM »
Yes those are the local variables.
This is just starter routine that needs some error traps.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Create command to draw an object..?
« Reply #16 on: June 05, 2008, 01:55:23 PM »
This is what I had in mind.
Code: [Select]
;;  CAB @ TheSwamp.org 06/05/2008
(Defun C:SQR (/ SquareWidth RoundWidth Run ptBase ll lr ur ul um acDoc obj1 obj2
              err lyrobj LayerName
             )
  (vl-load-com)
  ;;  CAB 05/31/07
  (defun activespace (doc)
    (if (or (= acmodelspace (vla-get-activespace doc))
            (= :vlax-true (vla-get-mspace doc))
        )
      (vla-get-modelspace doc)
      (vla-get-paperspace doc)
    )
  )
  (defun MakePline (space point-list)
    (vlax-invoke space
                 'AddLightWeightPolyline
                 (apply 'append point-list) ; 2D point list
    )
  )
;;  returns nil if make failed
(defun MakeLayer (lyrname acDoc / lyrobj)
  (vl-load-com)
  (if
    (not
      (vl-catch-all-error-p
        (setq lyrobj
          (vl-catch-all-apply
              'vla-add
              (list (vla-get-layers acDoc) lyrname))
        )
      )
    )
    lyrobj
  )
)
 
  (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))

  ;;  Create the layer
  (if (setq lyrobj (MakeLayer "Equip" acDoc))
    (progn
      (vla-put-color lyrobj "123")
      (vlax-release-object lyrobj)
      (setq LayerName "Equip")
    )
  )

  (if
    (and
      (setq SquareWidth (getdist "\nEnter the Square Width: "))
      (setq RoundWidth (getdist "\nEnter the Round Width: "))
      (setq Run (getdist "\nEnter the lenght: "))
    )
     (progn
       ;;  draw the object at 0,0 as the base point at middle of
       ;;  the square end, then move to desired location.
       (setvar "plinewid" 0)
       (setq ptBase '(0 0)
             ll     (polar ptBase pi (/ SquareWidth 2.))
             lr     (polar ptBase 0.0 (/ SquareWidth 2.))
             um     (polar ptBase (/ pi 2) Run)
             ul     (polar um pi (/ RoundWidth 2.))
             ur     (polar um 0.0 (/ RoundWidth 2.))
       )
       (if (setq obj1 (MakePline (activespace acDoc) (list ll lr ur ul)))
         (progn
           (vla-put-Closed Obj1 :vlax-true)
           (and LayerName (vla-put-Layer Obj1 LayerName))
           ;;(vla-put-Closed PolObj :vlax-true)
           ;;(vla-put-Color PolObj AcYellow)
           ;;(vla-put-Linetype PolObj "HIDDEN")
           (if (setq obj2 (MakePline (activespace acDoc) (list ll um lr)))
             (progn
               (and LayerName (vla-put-Layer Obj2 LayerName))
               (vla-put-Color Obj2 AcWhite)
               (setq err (vl-catch-all-apply
                           'vl-cmdf
                           (list "_.move"
                                 (vlax-vla-object->ename obj1)
                                 (vlax-vla-object->ename obj2)
                                 "" "_non" ptBase pause))
               )

             )
           )
           (if (< (distance ptBase (getvar "lastpoint")) 0.000001)
             ;;  objects were not moved, so erase them
             (progn
               (and obj1 (vla-erase obj1))
               (and obj2 (vla-erase obj2))
             )
           )

         )
       )

     )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Create command to draw an object..?
« Reply #17 on: June 05, 2008, 05:37:51 PM »
Here is another version where you select the base point of the object.

Code: [Select]
;;  CAB @ TheSwamp.org 06/05/2008
(Defun C:SQR (/ SquareWidth RoundWidth Run ptBase ll lr ur ul um acDoc obj1 obj2
              err lyrobj LayerName pp activespace MakePline MakeLayer
             )
  (vl-load-com)
  ;;  CAB 05/31/07
  (defun activespace (doc)
    (if (or (= acmodelspace (vla-get-activespace doc))
            (= :vlax-true (vla-get-mspace doc))
        )
      (vla-get-modelspace doc)
      (vla-get-paperspace doc)
    )
  )
  (defun MakePline (space point-list)
    (vlax-invoke space
                 'AddLightWeightPolyline
                 (apply 'append point-list) ; 2D point list
    )
  )
;;  returns nil if make failed
(defun MakeLayer (lyrname acDoc / lyrobj)
  (vl-load-com)
  (if
    (not
      (vl-catch-all-error-p
        (setq lyrobj
          (vl-catch-all-apply
              'vla-add
              (list (vla-get-layers acDoc) lyrname))
        )
      )
    )
    lyrobj
  )
)
 
  (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))

  ;;  Create the layer
  (if (setq lyrobj (MakeLayer "Equip" acDoc))
    (progn
      (vla-put-color lyrobj "123")
      (vlax-release-object lyrobj)
      (setq LayerName "Equip")
    )
  )

  (if
    (and
      (setq SquareWidth (getdist "\nEnter the Square Width: "))
      (setq RoundWidth (getdist "\nEnter the Round Width: "))
      (setq Run (getdist "\nEnter the lenght: "))
    )
     (progn
       (initget "0 1 2 3 4 5")
       (cond
         ((setq pp (getkword "\nRef Point on Duct.[0=MB 1=LL 2=LR 3=UR 4=UL 5=UM]<0>")))
         ((setq pp "0"))
       )
       ;;  draw the object at 0,0 as the base point at middle of
       ;;  the square end, then move to desired location.
       (setvar "plinewid" 0)
       (setq ptBase '(0 0)
             ll     (polar ptBase pi (/ SquareWidth 2.))
             lr     (polar ptBase 0.0 (/ SquareWidth 2.))
             um     (polar ptBase (/ pi 2) Run)
             ul     (polar um pi (/ RoundWidth 2.))
             ur     (polar um 0.0 (/ RoundWidth 2.))
       )
       ;;  redefine pp as the pick point
       (setq pp (eval (cdr (assoc pp '(("0" . ptBase)("1" . ll)("2" . lr)
                                       ("3" . ur)("4" . ul)("5" . um))))))
       (if (setq obj1 (MakePline (activespace acDoc) (list ll lr ur ul)))
         (progn
           (vla-put-Closed Obj1 :vlax-true)
           (and LayerName (vla-put-Layer Obj1 LayerName))
           ;;(vla-put-Closed PolObj :vlax-true)
           ;;(vla-put-Color PolObj AcYellow)
           ;;(vla-put-Linetype PolObj "HIDDEN")
           (if (setq obj2 (MakePline (activespace acDoc) (list ll um lr)))
             (progn
               (and LayerName (vla-put-Layer Obj2 LayerName))
               (vla-put-Color Obj2 AcWhite)
               (vl-catch-all-apply
                           'vl-cmdf
                           (list "_.move"
                                 (vlax-vla-object->ename obj1)
                                 (vlax-vla-object->ename obj2)
                                 "" "_non" pp pause))
               (if (> (distance ptBase (getvar "lastpoint")) 0.001)
                 (vl-catch-all-apply
                           'vl-cmdf
                           (list "_.rotate"
                                 (vlax-vla-object->ename obj1)
                                 (vlax-vla-object->ename obj2)
                                 "" "_non" (getvar "lastpoint") pause))
               )

             )
           )
           (if (< (distance ptBase (getvar "lastpoint")) 0.000001)
             ;;  objects were not moved, so erase them
             (progn
               (and obj1 (vla-erase obj1))
               (and obj2 (vla-erase obj2))
             )
           )

         )
       )

     )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CECE_CAD

  • Guest
Re: Create command to draw an object..?
« Reply #18 on: June 10, 2008, 01:21:35 PM »
Hey CAB...   With the one that you have the base point set to (0 0)

Code: [Select]
;;  draw the object at 0,0 as the base point at middle of
       ;;  the square end, then move to desired location.
       (setvar "plinewid" 0)
       (setq ptBase '(0 0)    <------  Can i change this to be upper left, like this : (setq ptBase '(ul)

?

CECE_CAD

  • Guest
Re: Create command to draw an object..?
« Reply #19 on: June 10, 2008, 01:22:52 PM »
By the way, the code works great.  Yeah, it would of taking me a long time to get this one...  Thank you, I was so stuck.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Create command to draw an object..?
« Reply #20 on: June 10, 2008, 01:31:32 PM »
You're welcome.

If you wnat to move based on the ul change this
Code: [Select]
(list "_.move"
                                 (vlax-vla-object->ename obj1)
                                 (vlax-vla-object->ename obj2)
                                 "" "_non" [color=red]ptBase[/color] pause))
To this
Code: [Select]
(list "_.move"
                                 (vlax-vla-object->ename obj1)
                                 (vlax-vla-object->ename obj2)
                                 "" "_non" [color=red]ul[/color]  pause))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CECE_CAD

  • Guest
Re: Create command to draw an object..?
« Reply #21 on: June 10, 2008, 01:37:25 PM »
Very nice...   :lol:  that is perfect