Author Topic: Couple of Active-X questions  (Read 2600 times)

0 Members and 1 Guest are viewing this topic.

Jeremy

  • Guest
Couple of Active-X questions
« on: June 21, 2012, 03:43:55 PM »
I am sure the masters of code here can point me in the right direction on these two questions.

1. Given the radius and two points defining the ends of a cylinder draw one using active-x. I know there is the vla-AddCylinder method but I do not see any means of setting the orientation. Must one invoke the UCS command?

2. I have a closed 3D polyline that I want to turn into a region using active-x. Suppose I have retrieved the pline using (entlast) and now want to feed it into vla-AddRegion, how do I do so? I have tried feeding it as a safearray but I can't seem to get the syntax quite right for it to be happy.

Thanks in advance!

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Couple of Active-X questions
« Reply #1 on: June 21, 2012, 05:34:06 PM »
1. Given the radius and two points defining the ends of a cylinder draw one using active-x. I know there is the vla-AddCylinder method but I do not see any means of setting the orientation. Must one invoke the UCS command?

Don't have much time to spend on this, but maybe something like:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:cyl ( / p q r v )
  2.     (if (and
  3.             (setq p (getpoint "\nSpecify Center of Base: "))
  4.             (progn
  5.                 (initget 6)
  6.                 (setq r (getdist p "\nSpecify Radius: "))
  7.             )
  8.             (setq q (getpoint p "\nSpecify Center of Opposite End: "))
  9.             (not (equal '(0.0 0.0 0.0) (setq v (mapcar '- p q)) 1e-8))
  10.         )
  11.         (progn
  12.             (vla-transformby
  13.                 (vla-addcylinder
  14.                     (vlax-get-property
  15.                         (vla-get-activedocument (vlax-get-acad-object))
  16.                         (if (= 1 (getvar 'cvport))
  17.                             'paperspace
  18.                             'modelspace
  19.                         )
  20.                     )
  21.                     (vlax-3D-point '(0.0 0.0 0.0)) r
  22.                     (distance p q)
  23.                 )
  24.                 (vlax-tmatrix
  25.                     (append
  26.                         (mapcar
  27.                             (function
  28.                                 (lambda ( a b )
  29.                                     (append (trans a 0 v t) (list b))
  30.                                 )
  31.                             )
  32.                            '(
  33.                                 (1.0 0.0 0.0)
  34.                                 (0.0 1.0 0.0)
  35.                                 (0.0 0.0 1.0)
  36.                             )
  37.                             (mapcar '(lambda ( a b ) (- a (/ b 2.0))) (trans p 1 0) v)
  38.                         )
  39.                        '((0.0 0.0 0.0 1.0))
  40.                     )
  41.                 )
  42.             )
  43.         )
  44.     )
  45. )

2. I have a closed 3D polyline that I want to turn into a region using active-x. Suppose I have retrieved the pline using (entlast) and now want to feed it into vla-AddRegion, how do I do so? I have tried feeding it as a safearray but I can't seem to get the syntax quite right for it to be happy.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:poly2reg ( / s )
  2.     (if (setq s (ssget "_+.:E:S:L" '((0 . "POLYLINE") (-4 . "&=") (70 . 9))))
  3.         (vla-addregion
  4.             (vlax-get-property
  5.                 (vla-get-activedocument (vlax-get-acad-object))
  6.                 (if (= 1 (getvar 'cvport))
  7.                     'paperspace
  8.                     'modelspace
  9.                 )
  10.             )
  11.             (vlax-make-variant
  12.                 (vlax-safearray-fill
  13.                     (vlax-make-safearray vlax-vbobject '(0 . 0))
  14.                     (list (vlax-ename->vla-object (ssname s 0)))
  15.                 )
  16.             )
  17.         )
  18.         (princ "\nInvalid Object or nothing selected.")
  19.     )
  20.     (princ)
  21. )

Of course, variants and safearrays could always be avoided by using vlax-invoke

Jeremy

  • Guest
Re: Couple of Active-X questions
« Reply #2 on: June 21, 2012, 08:23:19 PM »
Thanks a lot Lee. I was close on the region problem but I had '(0 . 1) in my array instead of '(0 . 0). Curse the inventor of counting indices beginning at 0!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Couple of Active-X questions
« Reply #3 on: June 21, 2012, 08:46:07 PM »
< .. > Curse the inventor of counting indices beginning at 0!

While I wait for my coffee to cool a little .. some history.

In most languages :-
Index counting beginning from 0 makes perfect sense when you consider this :

The name of the array usually contains a pointer to the first element of the array in memory.
The index represents the memory offsets away from the named variable memory location.
So,  Offset [n] will point to each element, with offset[0] (no offset) being the first element.

[/back to my coffee]
 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeremy

  • Guest
Re: Couple of Active-X questions
« Reply #4 on: June 22, 2012, 03:28:58 PM »
It may make perfect sense to a guru but not for ordinary people. It is bad human factors design to implement a system that is contrary to the way we all taught to count. When you are asked to count something do you begin at 0 or 1? And, if this is so logical then why is string indexing always beginning at 1 instead of 0, even worse human factor design to have two separate systems of counting.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Couple of Active-X questions
« Reply #5 on: June 22, 2012, 05:08:29 PM »
Most string indexing systems begin at 0 too.  Programming is aimed at programmers, who are usually far from ordinary.  Hex means something different to most folks.   ;-)
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Couple of Active-X questions
« Reply #6 on: June 22, 2012, 05:13:06 PM »
And, if this is so logical then why is string indexing always beginning at 1 instead of 0

This is not true in general,  C/C++ use character arrays which also use zero-based indexes, furthermore, the Visual LISP string functions use zero-based indexes. I personally prefer zero-based indexes, coming from a mathematical background where zero-based indexing is used all the time, you very quickly get accustomed to it.
« Last Edit: June 22, 2012, 05:16:38 PM by Lee Mac »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Couple of Active-X questions
« Reply #7 on: June 23, 2012, 02:34:26 AM »
What I do hate is inconsistency with zero/one based arrays. E.g. C uses zero based, Pascal uses one based (by default), VB can have either or, or even N-based. And lo-and-behold ... AutoLisp uses both 0 and 1:
Code - Auto/Visual Lisp: [Select]
  1. (nth 0 lst) ;Retireves the 1st item from a list
  2. (substr str 1 1) ;Retrieves the 1st character from a string
  3.  
And then just to confuse you more, the vl-string-* functions are zero-based again! That's what I mean by INCONSISTENCY! Either stick with one or the other, damit!
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Couple of Active-X questions
« Reply #8 on: June 23, 2012, 04:09:45 AM »
What I do hate is inconsistency with zero/one based arrays. E.g. C uses zero based, Pascal uses one based (by default), VB can have either or, or even N-based. And lo-and-behold ... AutoLisp uses both 0 and 1:
Code - Auto/Visual Lisp: [Select]
  1. (nth 0 lst) ;Retireves the 1st item from a list
  2. (substr str 1 1) ;Retrieves the 1st character from a string
  3.  
And then just to confuse you more, the vl-string-* functions are zero-based again! That's what I mean by INCONSISTENCY! Either stick with one or the other, damit!

++

IMO, VB is the more inconsistent language I know.
Speaking English as a French Frog

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Couple of Active-X questions
« Reply #9 on: June 23, 2012, 05:48:46 AM »
IMO, VB is the more inconsistent language I know.
Yep, that lower-bound and upper-bound idea is plain stupid when it comes to attempting to figure out what's going on. I mean, what's wrong with a + or a - to convert your counter to the zero based index? Why should you want to start an array at some arb number like -152348?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Couple of Active-X questions
« Reply #10 on: June 23, 2012, 05:58:38 AM »
@Jeremy: Yes, a zero based index is not "natural" for most people. There's not much in it though, e.g. it makes some coding a bit easier:
Code - Auto/Visual Lisp: [Select]
  1. (setq idx (sslength ss))
  2. (while (>= (setq idx (1- idx)) 0)
  3.   ;; Do some stuuff
  4. )
If it was one based, then you couldn't do that without first incrementing the idx:
Code - Auto/Visual Lisp: [Select]
  1. (setq idx (1+ (sslength ss)))
  2.  (while (>= (setq idx (1- idx)) 1)
  3.    ;; Do some stuuff
  4.  )

What I'm on about regarding AutoLisp's inconsistency with strings is this: Say you're using vl-string-search to find the position of a word inside some text. That would give you the 0-based index. But to extract that word using substr, you'll then always need to +1 onto the index - otherwise you get the character before the actual match.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.