Jump to content

Inserting a block in named UCS then moving it in WCS


Dorian Gray

Recommended Posts

The client has a a whole, heap of tags that need to be inserted into a 2d drawing.

Each drawings has a named UCS view, I'd like to insert the block so that it is perpendicular to the ucs.

 

What I was thinking of doing is insert the blocks (W2B_3D_ELEMENT_TAG) at 0,0 of the UCS the use (ssget "_C" '(-0.5 -40) '(0 -40)) to select the block. Change to WCS and move to the correct WCS.

 

Problem is it doesn't move the block from its insertion point it picks from an altogether different point.

 

is there a way of using ssget to pick from the blocks insertion point?

 

I know there is also a trans command but not sure how it works

(trans '(1.0 2.0 3.0) 0 1)

 

Any help/suggestions

 

Craig

Link to comment
Share on other sites

Hi Dorian!

 

What I was thinking of doing is insert the blocks (W2B_3D_ELEMENT_TAG) at 0,0 of the UCS the use (ssget "_C" '(-0.5 -40) '(0 -40)) to select the block. Change to WCS and move to the correct WCS. Problem is it doesn't move the block from its insertion point it picks from an altogether different point.

Ok, first off, it seems there is some missing information in what you say. I don't know how exactly you do your move, as you only talk about your ssget, which make a selection. One thing I can say is moving requires 2 points representing the movement vector. "From > To" is rather straight forward, and I'm not sure how you proceed if you say the block get picked by another point.

 

is there a way of using ssget to pick from the blocks insertion point?
First there would, but I wouldn't use ssget "_c" for that for many reasons: it would grab every other object within the selection zone, it can be picky because objects have to be visible on the screen, and also because of the mechanics of ssget "_c". While the window is orthogonal to wcs axis, which can sometimes be a pita, the coordinates are needed in the current ucs.

 

I know there is also a trans command but not sure how it works

(trans '(1.0 2.0 3.0) 0 1)

It translates a point from one coordinate system to another. trans . The From/to I most commonly use are 0 (wcs), 1 (current ucs) and . The last one will be used to transpose from/to any Object Coordinate Systems (OCS) which are arbitrary determined by AutoCAD. Let's say your current ucs is front and you have a point at coord (0 2 0) and would like to have its position relative to the WCS, you would use (trans '(0 2 0) 1 0), which would return (0.0 0.0 2.0).

 

Now, for a more practical use, I'll show you a way of using trans. Note: A block reference insertion point is stored in the assoc 10 but is in its OCS. Lets say I would like to move the block by x+1 y+1 z+1 in WCS here's how we could do it, step by step.

(defun c:test ()
(setq ChosenBlockEntsel (entsel "pick a block"));get the block
(setq ChosenBlockEname (car ChosenBlockEntsel));retrieve its <ename>
(setq ChosenBlockEList (entget ChosenBlockEname));retrieve the entity's list
(setq BInsertPointAssocList (assoc 10 ChosenBlockEList)); get the assoc of the OCS coordinate
(setq BInsertPointCoordOCS (cdr BInsertPointAssocList));retrieve the OCS coordinate
(setq BInsertPointCoordWCS (trans BInsertPointCoordOCS ChosenBlockEname 0));trans that coord to WCS
(setq NewBInsertPointCoordWCS (mapcar '+ '(1 1 1) BInsertPointCoordWCS));apply the wcs move vector
(setq NewBInsertPointCoordOCS (trans NewBInsertPointCoordWCS 0 ChosenBlockEname));trans that new coordinate in the block OCS
(setq NewBInsertPointAssocList (cons 10 NewBInsertPointCoordOCS));Rebuild the assoc 10 list with the new coordinate
(setq NewEListToEntmod (subst NewBInsertPointAssocList BInsertPointAssocList ChosenBlockEList));Rebuild the new block insert entity list
(entmod NewEListToEntmod); entmod it
(vl-cmdf "_.AttSync" "Select" ChosenBlockEname "Yes");needed if your block possess attributes.
)

Thats was old school style! Ok, now another and easier way to move the block would be the following. A lot easier (but still not the one I would necessarly use). Vlax-3d-point and vla-move don't require the use of trans, as the coordinate they use are always relative to the WCS. Be carefull if you ask some input of the user like with getpoint, as the value of getpoint is relative to the active UCS and would require the use of trans. Note that for the following example, I didn't ask any user input, and only move the block by 2 units in the x axis in the WCS (delta point2 point1). The only thing needed is to transfer the block ename to a VL-Object and we are good to go.

(defun c:test2()
   (setq acadObj (vlax-get-acad-object))
   (setq doc (vla-get-ActiveDocument acadObj))
   (setq ChosenBlockEntsel (entsel "pick a block"))
   (setq ChosenBlockEname (car ChosenBlockEntsel))
   (setq ChosenBlockVLObject (vlax-ename->vla-object ChosenBlockEname))
   ;; Define the points that make up the move vector
   (setq point1 (vlax-3d-point 0 0 0)
         point2 (vlax-3d-point 2 0 0))
   (vla-Move ChosenBlockVLObject point1 point2)
)

 

Ok, now that we had fun and know how things work, let's be more serious and more efficient. Instead of inserting the block at coordinate 0,0, then try to ssget the block just inserted (and only the block just inserted), to then move it from its insertion point to the new coordinate chosen by the user.... we can insert it directly at the desired coordinate, aligned with the current UCS, merci-bonsoir style! FTOW!

(defun c:InsTag ( / acadObj doc insertionPnt modelSpace rotationangle blockRefObj)
   ;Jef! 2016-03-01
   ;Nota: the block  "W2B_3D_ELEMENT_TAG" need to exist in the drawing
   (vl-load-com)
   (setq acadObj (vlax-get-acad-object))
   (setq doc (vla-get-ActiveDocument acadObj))
   (setq insertionPnt (vlax-3d-point (trans (getpoint "pick the coordinate for the insert point") 1 0)))
   (setq modelSpace (vla-get-ModelSpace doc))
   (setq rotationangle (angle '(0.0 0.0) (trans (getvar 'ucsxdir) 0 (trans '(0.0 0.0 1.0) 1 0 t) t)))
   (setq blockRefObj (vla-InsertBlock modelSpace insertionPnt "W2B_3D_ELEMENT_TAG" 1 1 1 rotationangle))
)

Just be sure the "W2B_3D_ELEMENT_TAG" block definition is in the drawing before you launch.. As active-X Ref Guide say :Attempting to call the InsertBlock method with an uninitialized Name parameter results in unexpected behavior.". Be sure to do everything you need not to break your cad. :P

 

Hope it helped

Cheers!

Jef!

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...