Jump to content

Mline to Pline


autolisper

Recommended Posts

Hi All,

I am trying to draw a pline by connecting all the vertices of the mline. Below is my code, but it gives an error saying;

error: bad argument type: fixnump: 

 

(vl-load-com)

(defun c:hl_mltopline ()
  (setq	mspace (vla-get-modelSpace
		 (vla-get-activeDocument
		   (vlax-get-acad-object)
		 )
	       )
  )

  (setq	ml (vlax-ename->vla-object
	     (car (entsel "\nPick a multiline: "))
	   )
  )
  (setq coord (vlax-safearray->list (vlax-variant-value (vla-get-coordinates ml))))
  
  (setq pl_points_safearray (vlax-make-safearray vlax-vbDouble pl_points_list))
  (setq pline (vla-addlightweightpolyline mspace ))
  (entmod pline)
)

 

Could someone please help me fix this? Thanks in advance..!!

Link to comment
Share on other sites

There are many issues with your code -

  • You define a variable 'coord' as the coordinates of the mline, but then supply vlax-make-safearray with the variable 'pl_points_list'.
  • You are not supplying all of the required arguments for the addlightweightpolyline method, only the block container argument 'mspace'.
  • The addlightweightpolyline method returns a vla-object, not an entity name which may be used with the entmod function.
  • You are not declaring your local variables.
  • You are not accounting for null user input.
  • You are not accounting for the user selecting objects other than multilines.

 

  • Like 1
Link to comment
Share on other sites

Try this, there are other ways like entmake.

(defun c:hl_mltopline ( / mspace ent coords pline)
  (while (and (setq ent (car (entsel "\nPick a multiline Enter to exit : ")))
         (= (cdr (assoc 0 (entget ent))) "MLINE")
         )
  (setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
  (setq coords (vlax-safearray->list (vlax-variant-value (vla-get-coordinates (vlax-ename->vla-object ent)))))
  (setq tmp (vlax-make-safearray vlax-vbDouble (cons 0 (- (length coords) 1))))
      (vlax-safearray-fill tmp coords)
      (setq myobj (vla-addPolyline mspace tmp))
  )
(princ)
)

Maybe do a search next time 1st this is 3 years old and has lee's solution as well.

 

Edited by BIGAL
Link to comment
Share on other sites

Dear Lee,

Though I had started to explore Autolisp some years back, I could not continue my learning. Now recently I restarted exploring and its hardly a couple of months. Thanks a lot for your time and effort for pointing out the mistakes and for the advise....

Link to comment
Share on other sites

Dear Bigal,

Thanks for the code. I tried it. It gives the following error;

error: lisp value has no coercion to VARIANT with this type: 

 

But your code below worked exactly as I wanted...

https://www.cadtutor.net/forum/topic/63755-lisp-to-convert-a-mline-to-a-single-pline/?do=findComment&comment=525461

 

Thanks a lot..!!

 

Edited by autolisper
Link to comment
Share on other sites

13 hours ago, autolisper said:

Dear Lee,

Though I had started to explore Autolisp some years back, I could not continue my learning. Now recently I restarted exploring and its hardly a couple of months. Thanks a lot for your time and effort for pointing out the mistakes and for the advise....

 

You're most welcome.

 

On this subject, you might find this program useful.

Link to comment
Share on other sites

Odd re HL_MLTOPLINE  above maybe its a Briscad thing and it works no errors draws a pline. I set the layer to another so could see it make pline ie not white colour.

 

image.png.3a9b16a90ca7cf99c33b84b0d15c6011.png

Edited by BIGAL
Link to comment
Share on other sites

What you have done is strange:

First you convert a variant to a list then you convert the list to a variant.

 

Also look at vlax-get/vlax-put.

Link to comment
Share on other sites

3 hours ago, Roy_043 said:

What you have done is strange:

First you convert a variant to a list then you convert the list to a variant.

 

To be fair to the OP, the safearray variants used by the coordinates property of an MLINE & LWPOLYLINE object are of different dimensions (the MLINE requires 3D coordinates, whereas the LWPOLYLINE requires 2D coordinates), and so the conversion (or some form of list manipulation if using vlax-get/vlax-put) would be required. BricsCAD may be more tolerant in this regard.

Link to comment
Share on other sites

23 minutes ago, Roy_043 said:

BricsCAD is not that tolerant. I was commenting on Bigal's revised code. The issue you mention does not apply there.

 

Sorry - I thought you were commenting on the OP's code, which performs the same conversion.

Link to comment
Share on other sites

Quick answer from me is that I would not normally use (vla-addPolyline rather entmake or a command method as the list is xyz xyz xyz I would just make a point using list of nth's.

Link to comment
Share on other sites

Code simplified per @Roy_043 suggestions.

(defun c:hl_mltopline (/ ent sp)
  (while (and (setq ent (car (entsel "\nPick a multiline Enter to exit : ")))
	      (= (cdr (assoc 0 (entget ent))) "MLINE")
	 )
    (or sp (setq sp (vlax-ename->vla-object (cdr (assoc 330 (entget ent))))))
    (vlax-invoke sp 'addpolyline (vlax-get (vlax-ename->vla-object ent) 'coordinates))
  )
  (princ)
)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...