Author Topic: Inches to lineweight - improvements to my code?  (Read 2538 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 352
Inches to lineweight - improvements to my code?
« on: November 05, 2009, 09:03:27 AM »
I am using the following to convert from inches to enumerated lineweights.  Surely there is a better way.
Code: [Select]
;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  Routine: InchesToLineWeight
  Purpose: Converts a distance, in inches, to a lineweight as used by
  active-x.
  Arguments: Inches signature #1 - real - the lineweight in inches
         signature #2 - string - the name of the lineweight, this
         is limited to "DEFAULT" "BYBLOCK" or "BYLAYER".
  Returns: The lineweight enumeration as stored in acad.
  -----------------------------------------------------------------------------
  If an invalid lineweight is specified an error message is returned as a
  string, beginning with "ERROR|"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;

(defun InchesToLineWeight(inches / RT)
  (setq
    temp (assoc inches '((0 0)(0.002 5)(0.004 9)(0.005 13)(0.006 15)(0.007 18)
(0.008 20)(0.01 25)(0.012 30)(0.014 35)(0.016 40)(0.02 50)
(0.021 53)(0.024 60)(0.028 70)(0.031 80)(0.035 90)(0.039 100)
(0.042 106)(0.047 120)(0.055 140)(0.062 158)(0.079 200)(0.083 211)
("DEFAULT" -3 ) ("BYBLOCK" -2 ) ("BYLAYER" -1 )
)
)
    )
  (if temp (cadr temp)
    (strcat
      "ERROR|InchesToLineWeight - Invalid lineweight argument ("
      (vl-prin1-to-string inches)
      ")"
      )
    )
  )

I have considered adding another option to allow rounding to the nearest valid value.

Mike

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Inches to lineweight - improvements to my code?
« Reply #1 on: November 05, 2009, 02:35:03 PM »
I think I would use integers in the pairs & convert the inches to integers,
(setq inches (fix (/ inches 1000)))
This would eliminate any near misses.
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.

mkweaver

  • Bull Frog
  • Posts: 352
Re: Inches to lineweight - improvements to my code?
« Reply #2 on: November 05, 2009, 11:20:52 PM »
Thanks for the suggestion, Cab.  This is what I ended up with:
Code: [Select]
;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  Routine: InchesToLineWeight
  Purpose: Converts a distance, in inches, to a lineweight as used by
  active-x.
  Arguments: Inches signature #1 - real - the lineweight in inches
         signature #2 - string - the name of the lineweight, this
  is limited to "DEFAULT" "BYBLOCK" or "BYLAYER".
  Returns: The lineweight enumeration as stored in acad.
  -----------------------------------------------------------------------------
  If an invalid lineweight is specified an error message is returned as a
  string, beginning with "ERROR|"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;
(defun InchesToLineWeight (inches / RT temp)
  (setq
    temp (assoc (fix (round (* inches 1000) 0))
'((0 . 0)(2 . 5)(4 . 9)(5 . 13)(6 . 15)(7 . 18)
  (8 . 20)(10 . 25)(12 . 30)(14 . 35)(16 . 40)
  (20 . 50)(21 . 53)(24 . 60)(28 . 70)(31 . 80)
  (35 . 90)(39 . 100)(42 . 106)(47 . 120)(55 . 140)
  (62 . 158)(79 . 200)(83 . 211)("DEFAULT" . -3)
  ("BYBLOCK" . -2)("BYLAYER" . -1)
)
)
  )
  (if temp
    (cdr temp)
    (strcat
      "ERROR|InchesToLineWeight - Invalid lineweight argument ("
      (vl-prin1-to-string inches)
      ")"
    )
  )
)


;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  Routine: Round
  Purpose: Round numbers to a specified number of significant digits
  Arguments: num - the number to be rounded, may be an integer or a real
decs - the number of decimal places desired.
  if decs is supplied as a real, it is rounded to the
  nearest integer and applied
--------------------------------------------------------------------------------
  Examples:
    num dec returns
    143.125 2 143.12
    143.125 1 143.1
    143.125 0 143.0
    143.125 -1 140
    143.125 -2 100
    143.125 -3 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;
(defun round (num ;the number to round
      decs ;the number of decimal places
      / temp1 temp2)
  (if (= 'REAL (type decs))
    (setq decs (round decs 0))
  ) ;_ end of if

  (setq
    temp1 (/ (fix (* num (expt 10.0 decs))) (expt 10.0 decs) 1.0)
    temp2 (/ (fix (* num (expt 10.0 (1+ decs))))
     (expt 10.0 (1+ decs))
     1.0
  ) ;_ end of /
    ;;round up or down?
    temp2 (if (> (/ 1 2.0) (* (- temp2 temp1) (expt 10.0 decs)))
    ;;round down
    temp1
    ;;round up
    (+ temp1 (/ 1.0 (expt 10.0 decs)))
  ) ;_ end of if
    ;;make it an integer?
    temp2 (if (> 0 decs)
    ;;yes
    (fix temp2)
    ;;no
    temp2
  ) ;_ end of if
  ) ;end setq
) ;_ end of defun

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Inches to lineweight - improvements to my code?
« Reply #3 on: November 05, 2009, 11:31:14 PM »
You'll need to deal with strings before sending the argument to (fix  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.

mkweaver

  • Bull Frog
  • Posts: 352
Re: Inches to lineweight - improvements to my code?
« Reply #4 on: November 06, 2009, 06:35:16 AM »
Oh.  Good point.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Inches to lineweight - improvements to my code?
« Reply #5 on: November 13, 2009, 12:08:18 PM »
None of this seems to work for me, but I am extremely interested in this, as it will help a lot in finally fixing one of the major issues we have here.

fixo

  • Guest
Re: Inches to lineweight - improvements to my code?
« Reply #6 on: November 14, 2009, 08:38:49 AM »
None of this seems to work for me, but I am extremely interested in this, as it will help a lot in finally fixing one of the major issues we have here.

Not sure about, maybe I'm wrong, so ignore this
Perhaps  might be:
Code: [Select]
.........)(cons (/ 83 [color=red]2540.[/color])  2.11)("DEFAULT" . -3)
  ("BYBLOCK" . -2)("BYLAYER" . -1)
)

~'J'~