Jump to content

Check if GetString Exist


gschmidt

Recommended Posts

Hi,

 

I want to create a Table on a certain Layout chosen by user

With a getstring I get the user input.

How can I loop through the layouts and if the Layout (name) not exist,

get a new user input (getstring) until the user typed an existing one?

 

This is what I have sofar, it only works when I type a Layout Name which exists:

 

(setq acadObj (vlax-get-acad-object))
(setq doc (vla-get-ActiveDocument acadObj))
(setq layouts (vla-get-Layouts doc))
	 
(setq tabname (getstring T "\nType Layout Name for Table [e.g. 01]: "))
		
(vlax-for tabs layouts 
(if (= (vla-get-Name tabs) tabname)
	(progn
		(vla-put-activelayout doc tabs)
		(setq pt (vlax-3d-point 312 289 0))
		(setq curspace (vla-get-paperspace doc))
		(setq objtable (vla-addtable curspace pt 11 10 4.652 9.356))
					
		(vla-setcelltextheight objtable 1 6 1.25)
		(vla-setcelltextheight objtable 1 7 1.25)
		(vla-setcelltextheight objtable 1 8 1.25)
		(vla-setcelltextheight objtable 1 9 1.25)
	)	
)
)

Link to comment
Share on other sites

Maybe something like this? (Untested & typed in post box!):

(defun c:test ( / lay lst obj )
   (setq lst (mapcar 'strcase (layoutlist)))
   (while
       (not
           (or
               (= "" (setq lay (getstring t "\nEnter layout for table: ")))
               (member (strcase lay) lst)
           )
       )
       (princ (strcat "\nLayout \"" lay "\" doesn't exist."))
   )
   (if (/= "" lay)
       (progn
           (setq obj 
               (vla-addtable 
                   (vla-get-block (vla-item (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) lay)) 
                   (vlax-3D-point 312 289)
                   11 10 4.652 9.356
               )
           )
           (foreach col '(6 7 8 9)
               (vla-setcelltextheight obj 1 col 1.25)
           )
       )
   )
   (princ)
)

Link to comment
Share on other sites

Exactly!....Thanx man, I was also playing with the vla-get-block, but I never could make it work.

 

Is it easy from here to include that: this table is only created if not present in the selected layout? to make it idiot-proof

Link to comment
Share on other sites

Exactly!....Thanx man, I was also playing with the vla-get-block, but I never could make it work.

 

You're welcome! :)

 

Is it easy from here to include that: this table is only created if not present in the selected layout? to make it idiot-proof

 

Not unless the table has some defining properties which the program could use in order to distinguish it from other possible tables residing in the target layout, for example, if the table were to reside on its own layer.

Link to comment
Share on other sites

Yes, each table is on a unique layer, so I can select it with?:

 

(setq table (ssget "x" '((0 . "ACAD_TABLE") (8 . "Layername"))))

 

However when I create a new table, a new layer will be created/set for the table. this happens with increment +1

e.g. the layer name starts with "29-anchor wires-1" and each time the LISP is performed the counter adds +1 to a new layer name if it already exists: "29-anchor wires-2", "29-anchor wires-3", etc.

So how would I know if a user randomly selects an existing Layout which already has a table, in which layer the Table is?

Link to comment
Share on other sites

Please try the following (untested) code:

(defun c:test ( / lay lst obj sel )
   (setq lst (mapcar 'strcase (layoutlist)))
   (while
       (and (/= "" (setq lay (getstring t "\nEnter layout for table: ")))
           (or
               (and
                   (not (member (strcase lay) lst))
                   (princ (strcat "\nLayout \"" lay "\" doesn't exist."))
               )
               (and
                   (setq sel (ssget "_X" (list '(0 . "ACAD_TABLE") '(8 . "29-anchor wires-#*") (cons 410 lay))))
                   (princ 
                       (strcat 
                           "\nA table already exists on layer \"" 
                           (cdr (assoc 8 (entget (ssname sel 0)))) 
                           "\" in layout \"" lay "\"."
                       )
                   )
               )
           )
       )
   )
   (if (/= "" lay)
       (progn
           (setq obj 
               (vla-addtable 
                   (vla-get-block (vla-item (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) lay)) 
                   (vlax-3D-point 312 289)
                   11 10 4.652 9.356
               )
           )
           (foreach col '(6 7 8 9)
               (vla-setcelltextheight obj 1 col 1.25)
           )
       )
   )
   (princ)
)

Link to comment
Share on other sites

Mmmm, I didn't test it idiot proof.

The code creates the first Table and if the layout not exist it returns the message and new input.

However the second Table is not drawn....; error: bad argument type: stringp nil

It crashes even before i can give the Layout Name input

 

Maybe something like this? (Untested & typed in post box!):

(defun c:test ( / lay lst obj )
   (setq lst (mapcar 'strcase (layoutlist)))
   (while
       (not
           (or
               (= "" (setq lay (getstring t "\nEnter layout for table: ")))
               (member (strcase lay) lst)
           )
       )
       (princ (strcat "\nLayout \"" lay "\" doesn't exist."))
   )
   (if (/= "" lay)
       (progn
           (setq obj 
               (vla-addtable 
                   (vla-get-block (vla-item (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) lay)) 
                   (vlax-3D-point 312 289)
                   11 10 4.652 9.356
               )
           )
           (foreach col '(6 7 8 9)
               (vla-setcelltextheight obj 1 col 1.25)
           )
       )
   )
   (princ)
)

Edited by gschmidt
Link to comment
Share on other sites

Mmmm, I didn't test it idiot proof.

The code creates the first Table and if the layout not exist it returns the message and new input.

However the second Table is not drawn....; error: bad argument type: stringp nil

It crashes even before i can give the Layout Name input

 

Second table?? :huh:

Link to comment
Share on other sites

In the attached drawing is a BLOCK of a Pipeline Installation Barge.

This Barge has 10 anchor wires with anchors to keep the Barge at its place during pipe lay.

Our Engineer calculates the length and angles of the anchor wires in special software, written in an XLSX table.

 

The LISP must draw the anchor wires, anchors and midline buoys, based on the XLSX sheet.

And put the XLSX values + the coordinates (WCS which the LISP calculates) of the endpoint of each anchor wire to a Table in the LAYOUT.

 

There are multiple layouts in the drawing, because it is a sequence drawing of multiple locations of the Barge.

Each location has its own LAYOUT and also its own Table.

Stingray for Anchors-TV-01.dwg

Anchors.lsp

Anchor Pattern.xlsx

Link to comment
Share on other sites

Yes you are right, your stand alone code works (forgot to test it stand alone) and when I insert it in my program it crashes the second time....hmmm.

Need to figure out what causes that, because it didn't crash without the table part.

Link to comment
Share on other sites

I figured out the problem, but not the answer.

Before the Table is created a Table_Style is created.

This code (build with trial and error) causes the error.

It should also check if the table_style already exist, but somewhere it crashes

the second time i call the subfunction.

 

(defun CTS (/ acmcol adoc clsname keyname newstyleobj tbldict tblstylename)
;(vl-load-com)
(or adoc
	(setq adoc
		(vla-get-activedocument
			(vlax-get-acad-object)
		)
	)
)
(setq	tbldict
	(vla-item
		(vla-get-dictionaries
			(vla-get-database adoc)
		)
		"Acad_TableStyle"
	)
)
 ;; look in the tablestyle dictionary to search for our style name:
(if 
	(vl-catch-all-error-p
		(setq tbstyleObj
			(vl-catch-all-apply
				(function (lambda() (vla-item tbldict "Anchor Table")))
			)
		)
	)
   	;; if table style "TblStyleName" does not exist:
	(progn
		(setq keyname "NewStyle"
					clsname "AcDbTableStyle"
					tblstylename "Anchor Table"
		)
		(setq	newstyleobj
				(vlax-invoke tbldict 'Addobject keyname clsname)
		)
		(setq acmcol (vla-GetInterfaceObject
							(vlax-get-acad-object)
							(strcat "AutoCAD.AcCmColor." (itoa (atoi(getvar "acadver"))))
						 )
		)
		(vlax-put acmcol 'Colorindex 254)
		(vlax-put newstyleobj 'Name TblStyleName)
		(vlax-put newstyleobj 'Description "Anchor Table")
		(vlax-put newstyleobj 'HorzCellMargin 0.25)
		(vlax-put newstyleobj 'VertCellMargin 0.25)
		(vlax-put newstyleobj 'TitleSuppressed :vlax-false)
		(vlax-put newstyleobj 'HeaderSuppressed :vlax-false)
		(vlax-invoke newstyleobj 'SetBackgroundColor acTitleRow acmcol)
		(vlax-invoke newstyleobj 'SetBackgroundColorNone acDataRow :vlax-false)
		(vlax-invoke newstyleobj 'SetGridLineWeight acHorzBottom acTitleRow acLnWt035)
		(vlax-invoke newstyleobj 'SetTextStyle (+ acHeaderRow acTitleRow) "Arial-B")
		(vlax-invoke newstyleobj 'SetTextStyle acDataRow "Arial")
		(vlax-invoke newstyleobj 'SetTextHeight acTitleRow 2.5)			
		(vlax-invoke newstyleobj 'SetTextHeight (+ acDataRow acHeaderRow) 1.5)
		(vlax-invoke newstyleobj 'SetGridVisibility acHorzInside  (+ acDataRow acHeaderRow) :vlax-true)
		(vlax-invoke newstyleobj 'SetAlignment (+ acDataRow acTitleRow) acMiddleCenter)
		(vlax-invoke newstyleobj 'SetAlignment acHeaderRow acMiddleCenter)

	)
   	;;inform user if style exist:
   	(princ (strcat "\nStyle \"" tblstylename "\" already exist."))
  )
  (setvar "ctablestyle" "Anchor Table")
(princ)
)

 

The function is called just before the table is created:

 

(defun c:test ( / lay lst obj )
(vl-load-com)
(CTS)
(setq lst (mapcar 'strcase (layoutlist)))
(while
	(and (/= "" (setq lay (getstring t "\nEnter layout for table: ")))
		(or
			(and
				(not (member (strcase lay) lst))
					(princ (strcat "\nLayout \"" lay "\" doesn't exist."))
				)
			(and
				(setq sel (ssget "_X" (list '(0 . "ACAD_TABLE") '(8 . "29-anchor wires-#*") (cons 410 lay))))
				(princ 
					(strcat 
						"\nA table already exists on layer \"" 
						(cdr (assoc 8 (entget (ssname sel 0)))) 
						"\" in layout \"" lay "\"."
					)
				)
			)
		)
	)
)
			
   (if (/= "" lay)
       (progn
           (setq objtable 
               (vla-addtable 
                   (vla-get-block (vla-item (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) lay)) 
                   (vlax-3D-point 312 289)
                   11 10 4.652 9.356
               )
           )
           (foreach col '(6 7 8 9)
               (vla-setcelltextheight objtable 1 col 1.25)
           )
		(vla-settext objtable 0 0 "STINGRAY ANCHORS")
		(vla-settext objtable 1 1 "EASTING")
		(vla-settext objtable 1 3 "NORTHING")
		(vla-settext objtable 1 5 "ANGLE")
		(vla-settext objtable 1 6 "LENGTH ANCHORE WIRE")
		(vla-settext objtable 1 7 "MIDLINE BUOY 1 *")
		(vla-settext objtable 1 8 "MIDLINE BUOY 2 *")
		(vla-settext objtable 1 9 "MIDLINE BUOY 3 *")
		(vla-MergeCells objtable 1 1 1 2)
		(vla-MergeCells objtable 1 1 3 4)
		(vla-put-HorzCellMargin objtable 0.25)
		(vla-put-VertCellMargin objtable 0.25)  
		(vla-setrowheight objtable 1 10)
		(vla-setcolumnwidth objtable 0 15.182); 0 is first column
		(vla-setcolumnwidth objtable 1 4.756)
		(vla-setcolumnwidth objtable 2 13.956)
		(vla-setcolumnwidth objtable 3 4.756)
		(vla-setcolumnwidth objtable 4 13.956)
		(vla-setcolumnwidth objtable 5 9.969)
		(foreach col '(6 7 8 9)
			(vla-setcolumnwidth objtable col 9.356)
           )
       )
)
(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...