vlax-safearray-put-element -- Error Bad argument type

vlax-safearray-put-element -- Error Bad argument type

leeminardi
Mentor Mentor
651 Views
8 Replies
Message 1 of 9

vlax-safearray-put-element -- Error Bad argument type

leeminardi
Mentor
Mentor

I'm tying to create a revolved solid from  a list of LISP generated points that will define a polyline from which a region is defined.  The program creates the polyline but throws an error at line 32 - 

"Points = #<safearray...>; error: bad argument type: safearrayp nil"

 

What is the correct code for this task?

 

Thank you.

(vl-load-com)
(defun c:Example_myRevolvedSolid ()
  (setq acadObj (vlax-get-acad-object))
  (setq doc (vla-get-ActiveDocument acadObj))

  (setq p1 '(5 2 0))
  (setq	w   7.0
	h   3.0
  )
  (setq points (vlax-make-safearray vlax-vbDouble '(0 . 14)))
   (vlax-safearray-fill points  (list (car p1)
		     (cadr p1)
		     (caddr p1)
		     (+ (car p1) w)
		     (cadr p1)
		     (caddr p1)
		     (+ (car p1) w)
		     (+ (cadr p1) h)
		     (caddr p1)
		     (car p1)
		     (+ (cadr p1) h)
		     (caddr p1)
		     (car p1)
		     (cadr p1)
		     (caddr p1)
	       
  ))

  ;; Create a lightweight Polyline object in model space
  (setq modelSpace (vla-get-ModelSpace doc))
  (setq plineObj (vla-AddPolyline modelSpace points))
  (vlax-safearray-put-element curves 0 plineObj)  ;<------- bad argument type
  
  (setq regionObj (vla-AddRegion modelSpace curves))
    (vla-ZoomAll acadObj)
    (alert "Revolve the region to create the solid.")
    
    ;; Define the rotation axis
    (setq rotAxisPt (vlax-3d-point 0 0 0)
          rotAxisDir (vlax-3d-point 1 0 0)
          rotAngle 180)
        
    ;; Create the solid
    (setq solidObj (vla-AddRevolvedSolid modelSpace (vlax-safearray-get-element (vlax-variant-value regionObj) 0) rotAxisPt rotAxisDir rotAngle))

    (setq NewDirection (vlax-3d-point -1 -1 1))
    (setq activeViewport (vla-get-ActiveViewport doc))
    (vla-put-Direction activeViewport NewDirection)
    (vla-put-ActiveViewport doc activeViewport)
    (vla-ZoomAll acadObj)
    (alert "Solid created.")
  
)

 

lee.minardi
0 Likes
Accepted solutions (1)
652 Views
8 Replies
Replies (8)
Message 2 of 9

Moshe-A
Mentor
Mentor

@leeminardi hi,

 

it is not enough to create points as safe array but need to be a variant

check this fix

 

enjoy

moshe

 

(setq points (vlax-make-safearray vlax-vbDouble '(0 . 14)))
  (setq points (vlax-safearray-fill points  (list (car p1)
		     (cadr p1)
		     (caddr p1)
		     (+ (car p1) w)
		     (cadr p1)
		     (caddr p1)
		     (+ (car p1) w)
		     (+ (cadr p1) h)
		     (caddr p1)
		     (car p1)
		     (+ (cadr p1) h)
		     (caddr p1)
		     (car p1)
		     (cadr p1)
		     (caddr p1)
	       
  )))
  
  (setq var (vlax-make-variant points))
  ;; Create a lightweight Polyline object in model space
  (setq modelSpace (vla-get-ModelSpace doc))
  (setq plineObj (vla-AddPolyline modelSpace var ;|points|;))
  (vlax-safearray-put-element curves 0 var plineObj)  ;<------- bad argument type

 

 

Message 3 of 9

komondormrex
Mentor
Mentor
Accepted solution

@leeminardi 

just add this line right before line 32

(setq curves (vlax-make-safearray vlax-vbobject '(0 . 0)))

 

0 Likes
Message 4 of 9

marko_ribar
Advisor
Advisor

It took me some spare time and I think that you want something like this...

 

(defun c:Example_myRevolvedSolid ( / acadObj doc p1 w h points modelSpace plineObj rotAxisPt rotAxisDir rotAngle solidObj NewDirection activeViewport )

  (or (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-get-acad-object) nil))) (vl-load-com))

  (setq acadObj (vlax-get-acad-object))
  (setq doc (vla-get-ActiveDocument acadObj))

  (setq p1 (list 5.0 2.0 0.0))
  (setq	w 7.0 h 3.0)
  (setq points (vlax-make-safearray vlax-vbDouble '(0 . 14)))
  (vlax-safearray-fill points
    (list
      (car p1)
      (cadr p1)
      (caddr p1)
      (+ (car p1) w)
      (cadr p1)
      (caddr p1)
      (+ (car p1) w)
      (+ (cadr p1) h)
      (caddr p1)
      (car p1)
      (+ (cadr p1) h)
      (caddr p1)
      (car p1)
      (cadr p1)
      (caddr p1)
    )
  )
  ;; Create a lightweight Polyline object in model space
  (setq modelSpace (vla-get-ModelSpace doc))
  (setq plineObj (vla-AddPolyline modelSpace points))
  (setq regionObj (car (vlax-invoke modelSpace (quote addregion) plineObj)))
  (if (and plineObj (= (type plineObj) (quote vla-object)) (not (vlax-erased-p plineObj)))
    (entdel (vlax-vla-object->ename plineObj))
  )
  (vla-ZoomAll acadObj)
  (alert "Revolve the region to create the solid.")
  ;; Define the rotation parameters
  (setq rotAxisPt (vlax-3d-point 0.0 0.0 0.0)
        rotAxisDir (vlax-3d-point 1.0 0.0 0.0)
        rotAngle pi
  )
  ;; Create the solid
  (setq solidObj (vla-AddRevolvedSolid modelSpace regionObj rotAxisPt rotAxisDir rotAngle))
  (if (and regionObj (= (type regionObj) (quote vla-object)) (not (vlax-erased-p regionObj)))
    (entdel (vlax-vla-object->ename regionObj))
  )
  (setq NewDirection (vlax-3d-point -1 -1 1))
  (setq activeViewport (vla-get-ActiveViewport doc))
  (vla-put-Direction activeViewport NewDirection)
  (vla-put-ActiveViewport doc activeViewport)
  (vla-ZoomAll acadObj)
  (alert "Solid created.")
  (princ)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 5 of 9

leeminardi
Mentor
Mentor

@Moshe-A Thank you for your response.  It was not clear to me how to make your line 23 complete. 

@komondormrex Thank you.  It works so I will go with your solution.

@marko_ribar Thank you as well although your code crashed my AutoCAD 2024!

 

I'm still a newbie to this VLA/VLAX world.

 

Lee

lee.minardi
0 Likes
Message 6 of 9

komondormrex
Mentor
Mentor

@leeminardi wrote:

I'm still a newbie to this VLA/VLAX world.


you are double welcome.

and if you are dealing with filling a safe array, it should be, unlike just variable, declared before hand. that's it.   

0 Likes
Message 7 of 9

marko_ribar
Advisor
Advisor

@leeminardi 

You are right... My code crashed my both AutoCAD 2025 and AutoCAD 2022, but it worked well in BricsCAD V25...

Nevertheless, I hope you can pull something from my version as well... (for ex. (setq RotAngle 180) should be (setq RotAngle pi)...)...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 8 of 9

marko_ribar
Advisor
Advisor

Hi, @leeminardi 

It's me again... Try this revision that should work well on AutoCAD...

(defun c:Example_myRevolvedSolid ( / acadObj doc p1 w h points modelSpace plineObj curves regionObj rotAxisPt rotAxisDir rotAngle solidObj NewDirection activeViewport )

  (or (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-get-acad-object) nil))) (vl-load-com))

  (setq acadObj (vlax-get-acad-object))
  (setq doc (vla-get-ActiveDocument acadObj))

  (setq p1 (list 5.0 2.0 0.0))
  (setq	w 7.0 h 3.0)

  (setq points (vlax-make-safearray vlax-vbDouble '(0 . 11)))
  (vlax-safearray-fill points
    (list
      (car p1)
      (cadr p1)
      (caddr p1)
      (+ (car p1) w)
      (cadr p1)
      (caddr p1)
      (+ (car p1) w)
      (+ (cadr p1) h)
      (caddr p1)
      (car p1)
      (+ (cadr p1) h)
      (caddr p1)
    )
  )
  ;; Create a lightweight Polyline object in model space
  (setq modelSpace (vla-get-ModelSpace doc))
  (setq plineObj (vla-AddPolyline modelSpace points))
  (vla-put-closed plineObj :vlax-true)
  (setq curves (vlax-make-safearray vlax-vbObject '(0 . 0)))
  (vlax-safearray-put-element curves 0 plineObj)
  (setq regionObj (vla-AddRegion modelSpace curves))
  (if (and plineObj (= (type plineObj) (quote vla-object)) (not (vlax-erased-p plineObj)))
    (progn
      (vla-delete plineObj)
      (vlax-release-object plineObj)
    )
  )
  (vla-ZoomAll acadObj)
  (alert "Revolve the region to create the solid.")
  ;; Define the rotation parameters
  (setq rotAxisPt (vlax-3d-point 0.0 0.0 0.0)
        rotAxisDir (vlax-3d-point 1.0 0.0 0.0)
        rotAngle pi
  )
  ;; Create the solid
  (setq regionObj (car (safearray-value (variant-value regionObj))))
  (setq solidObj (vla-AddRevolvedSolid modelSpace regionObj rotAxisPt rotAxisDir rotAngle))
  (if (and regionObj (= (type regionObj) (quote vla-object)) (not (vlax-erased-p regionObj)))
    (progn
      (vla-delete regionObj)
      (vlax-release-object regionObj)
    )
  )
  (setq NewDirection (vlax-3d-point -1 -1 1))
  (setq activeViewport (vla-get-ActiveViewport doc))
  (vla-put-Direction activeViewport NewDirection)
  (vla-put-ActiveViewport doc activeViewport)
  (vla-ZoomAll acadObj)
  (alert "Solid created.")
  (princ)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 9 of 9

leeminardi
Mentor
Mentor

@marko_ribar  It works. It looks like you added the statment suggested by @komondormrex.  Did you make other changes besides specifying the angle in radians? Thank you.

Lee 

lee.minardi
0 Likes