Author Topic: 3dsolid Face Midpoint  (Read 2950 times)

0 Members and 1 Guest are viewing this topic.

ScottMC

  • Newt
  • Posts: 191
3dsolid Face Midpoint
« on: May 11, 2020, 03:44:21 PM »
Here's a quickie.. 
  Got this code working except it declines/ignores on including the sides or non-WCS faces resulting in the point in the wrong spot and orientation. The desire is to put a point [for snap] at the center of a solid face. Running A2K. Currently my tool [2 point circle] prompts for two points, draws a circle then converts the circle into a point. Option would be to prompt selection of a face to switch the UCS to the Solid Face, as I use when editing a solid side, requiring a "shift-to-deselect" which would work excellent, then get the point to have a better OSD to use in the command. Just to DEEP for me. Thanks 
Code: [Select]
(defun c:scp  (/ e i s osm)
  (setvar 'cmdecho 0)
  (setq osm (getvar 'osmode))
  (setvar 'osmode 7)
  (princ "\n   Draw Point at Center: <Select Two Snaps:>")
  (vl-cmdf "circle" "2P" pause pause)
;(command)
;(if (setq s (ssget "_:L" '((0 . "CIRCLE"))))
  (setq s (ssget "L"))
  (repeat (setq i (sslength s))
    (setq e (ssname s (setq i (1- i))))
    (if (entmake (list '(0 . "POINT") (assoc 10 (entget e))))
      (entdel e)
      )
    )
  (setvar 'cmdecho 1)
  (setvar 'osmode osm)
  (princ)
  )
« Last Edit: May 11, 2020, 04:00:30 PM by ScottMC »

DEVITG

  • Bull Frog
  • Posts: 479
Re: 3dsolid Face Midpoint
« Reply #1 on: May 12, 2020, 07:12:14 PM »
Please upload such sample.dwg
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: 3dsolid Face Midpoint
« Reply #2 on: May 13, 2020, 02:34:31 AM »
Maybe try this line:
(vl-cmdf "_.point" "_m2p" pause pause)

ScottMC

  • Newt
  • Posts: 191
Re: 3dsolid Face Midpoint
« Reply #3 on: May 25, 2020, 07:42:25 PM »
Closer to perfection.. Got it working but would rather it be a cleaner operation as far as "Next" flip included with the UCS Face tool offers. When I try to do a "Next" face, it replies with:

Command:
SCP Select Face for Center Point:
Yes or No, please.

Yes or No, please.


SCP Unknown command "SCP".  Press F1 for help.


In the above, I hit "enter/rt-clk" four times then allows me to finish and completes correctly.
What do I do to get it to accept the "next"?
Thanks to any who can help!

Lisp updated and much better!

« Last Edit: June 09, 2020, 11:34:28 AM by ScottMC »

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: 3dsolid Face Midpoint
« Reply #4 on: June 05, 2020, 11:33:29 AM »
Give this a try - it's working for me.


Code: [Select]

(defun c:scp (/ p1 p2 e i s osm mucs) ;; Next glitch not fixed yet.. 5-25-20
  (vl-load-com)
  (defun q:geo:mid (p1 p2)
    (mapcar '(lambda (x) (/ x 2.)) (mapcar '+ p1 p2))
  )
  (setvar 'cmdecho 0)
  (setq osm (getvar 'osmode))
  (setvar 'osmode 7)
  (vl-cmdf "._ucs" "_save" "mucs")   ;;save current ucs
  (princ "Select Face for Center Point:")
 ;(princ "\nEnter an option [Next/Xflip/Yflip] <accept>:") wish this would be working
  ;; but just flipping the face choice "Next" is the point
  (command "._ucs" "_fa")   ;; get face ucs to place point
  ;; LOOP PAUSE UNTIL COMMAND IS COMPLETED
  (while (= (logand (getvar "cmdactive") 1) 1)
(command pause)
)
  (princ)
  (princ "\n   Draw Point at Center: <Select Two Snaps:>")
  (setq p1 (getpoint "\nPick or enter first point : "))
  (setq p2 (getpoint p1 "\nPick or enter second point: "))
  (setq s (q:geo:mid p1 p2))
  (vl-cmdf "._point" s)
  (vl-cmdf "._ucs" "_restore" "mucs")
  (vl-cmdf "._ucs" "_delete" "mucs")
  (setvar 'cmdecho 1)
  (setvar 'osmode osm)
  (princ)
)
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

ScottMC

  • Newt
  • Posts: 191
Re: 3dsolid Face Midpoint
« Reply #5 on: June 05, 2020, 04:59:58 PM »
That helps for sure. Thanks PKENEWELL BTW my sisters live in GR.
Now I'm going to continue my effort and try to safeguard against an
early command cancel.
-> Only other would be to actually eliminate the
two point method to obtain that face center as the two points ignore
the actual surface center. I have one separate that gets the center of a region
but...integrating is not easy. Guess I'll dig more into this. One I won't forget.
« Last Edit: June 09, 2020, 11:28:09 AM by ScottMC »

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: 3dsolid Face Midpoint
« Reply #6 on: June 09, 2020, 01:56:36 PM »
That helps for sure. Thanks PKENEWELL BTW my sisters live in GR.
Now I'm going to continue my effort and try to safeguard against an
early command cancel.
-> Only other would be to actually eliminate the
two point method to obtain that face center as the two points ignore
the actual surface center. I have one separate that gets the center of a region
but...integrating is not easy. Guess I'll dig more into this. One I won't forget.

Hi ScottMC. Glad to help. I am not sure how to determine the geometric center of a 3D solid face either - I'll have to look into it.
Hope your sisters are all OK - I heard there was some rioting in GR and I hope it didn't affect them.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: 3dsolid Face Midpoint
« Reply #7 on: June 09, 2020, 05:36:49 PM »
Hey ScottMC!

I believe I have a solution to eliminate the picking 2 points part. Try the code below. I am using the SOLIDEDIT command to copy the face, then getting the centroid of the resulting region. Afterward I delete the copied face. It works for the most part, but it cannot determine the face to copy if you flip the face; need to make sure the face is selected right the first time so I eliminated the Xflip/Yflip/Accept loop. I haven't figured out a solution yet to workaround this problem, but it seems to work pretty good for me.

Code: [Select]
(defun c:scp (/ fa cen s cmd *error*) ;; Next glitch not fixed yet.. 5-25-20
  (vl-load-com)
 
  (defun *error* ()
     (command)
     (vla-EndUndoMark (vla-get-activedocument (vlax-get-acad-object)))
     (if cmd (setvar 'cmdecho cmd))
     (if (not (vlax-erased-p fa))(vla-delete fa))
     (if (tblsearch "UCS" "mucs")
       (progn (vl-cmdf "._ucs" "_restore" "mucs")(vl-cmdf "._ucs" "_delete" "mucs"))
     )
  )

  (vla-StartUndoMark (vla-get-activedocument (vlax-get-acad-object)))
  (setq cmd (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (vl-cmdf "._ucs" "_save" "mucs");;save current ucs
  (princ "\nSelect Face for Center Point: ")
  (command "._ucs" "_fa" pause "")   ;; get face ucs to place point
  (vl-cmdf "._solidedit" "_F" "_C" (getvar 'lastpoint) "" "0,0,0" "@" "" "")
  (setq fa  (vlax-ename->vla-object (entlast))
        cen (vla-get-centroid fa)
        s   (vlax-safearray->list (vlax-variant-value cen))
  )
  (vla-delete fa)
  (vl-cmdf "._point" "_non" s)
  (vl-cmdf "._ucs" "_restore" "mucs")
  (vl-cmdf "._ucs" "_delete" "mucs")
  (setvar 'cmdecho cmd)
  (vla-EndUndoMark (vla-get-activedocument (vlax-get-acad-object)))
  (princ)
)

"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

ScottMC

  • Newt
  • Posts: 191
Re: 3dsolid Face Midpoint
« Reply #8 on: June 09, 2020, 07:20:11 PM »
Thank you so much PKENEWELL. This will certainly be used further in my drawing as solids are a primary. Hopes are others can use this too.  :angel: After trying seems I need to put the " (vl-cmdf "._ucs" "_delete" "mucs") "  back at the top to help clear that variable. When it errors on a face pointing away, it refuses to place a point. The (vla-delete fa) doesn't delet as desired..
« Last Edit: June 09, 2020, 09:13:21 PM by ScottMC »

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: 3dsolid Face Midpoint
« Reply #9 on: June 10, 2020, 04:26:09 PM »
No problem ScottMC. I hope it helps some. A couple things to note:

1) "(vla-delete fa)" is for deleting the copied face, not the "mucs" view, so don't remove it. There are some instances where the face selection doesn't work right unfortunately. I have not figured out a way around it. The best thing to do is to try to select the face as close to the center as possible. Perhaps someone else here can take a look and see if there is a better way.

2) I added an error function locally to delete the "mucs" named UCS if an error occurs. If this error handler is not working, then I will have to take a look and determine why.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

ScottMC

  • Newt
  • Posts: 191
Re: 3dsolid Face Midpoint
« Reply #10 on: June 12, 2020, 07:16:23 PM »
No problem ScottMC. I hope it helps some. A couple things to note:

1) "(vla-delete fa)" is for deleting the copied face, not the "mucs" view, so don't remove it. There are some instances where the face selection doesn't work right unfortunately. I have not figured out a way around it. The best thing to do is to try to select the face as close to the center as possible. Perhaps someone else here can take a look and see if there is a better way.

2) I added an error function locally to delete the "mucs" named UCS if an error occurs. If this error handler is not working, then I will have to take a look and determine why.

My A2K isn't removing the regions made.. Still would be nice to use what you did so it would be easier.

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: 3dsolid Face Midpoint
« Reply #11 on: June 18, 2020, 10:16:54 AM »
My A2K isn't removing the regions made.. Still would be nice to use what you did so it would be easier.

Hi ScottMC. Here's a program update, and please note the following:

1) I have corrected the error handler on the program. I made a couple mistakes in it previously. Forgot to include the "msg" argument and use "Command-s" for the ucs restore and delete. The error handler will also close the undo loop so that everything done in the last SCP command call will undo. It should also eliminate the named "mucs" left over. Unfortunately the error handler will not delete extra regions that are created from a selection that causes multiple faces to be created. just UNDO and re-start the command.

2) When you select faces - try NOT to select an edge (whether on the face or behind it). This is what causes the program to get confused and creates multiple faces, and depending on the current UCS - will either error out, or only add a point to the face that matches the UCS. IF this happens when you are using this program, just UNDO and re-select the face in a different spot.

Code: [Select]
(defun c:scp (/ fa cen s cmd *error*)
  (vl-load-com)

  (defun *error* (msg)
     (if (not (wcmatch (strcase msg T) "*break*,*cancel*,*quit*,*exit*"))
     (princ (strcat "\nError: " msg "\n"))
        (princ "\nProgram Aborted.\n")
     )
     (while (equal 8 (logand 8 (getvar "undoctl")))
        (vla-EndUndoMark (vla-get-activedocument (vlax-get-acad-object)))
     )
     (while (not (equal (getvar "cmdnames") ""))(command-s))
     (if (not (vlax-erased-p fa))(vla-delete fa))
     (if (tblsearch "UCS" "mucs")
       (progn (command-s "._ucs" "_restore" "mucs")(command-s "._ucs" "_delete" "mucs"))
     )
     (if cmd (setvar 'cmdecho cmd))
  )

  (vla-StartUndoMark (vla-get-activedocument (vlax-get-acad-object)))
  (setq cmd (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (vl-cmdf "._ucs" "_save" "mucs");;save current ucs
 
  (princ "\nSelect Face for Center Point: ")
  (command "._ucs" "_fa" pause "")   ;; get face ucs to place point
  (vl-cmdf "._solidedit" "_F" "_C" (getvar 'lastpoint) "" "0,0,0" "@" "" "")

  (setq fa  (vlax-ename->vla-object (entlast))
        cen (vla-get-centroid fa)
        s   (vlax-safearray->list (vlax-variant-value cen))
  )
  (vl-cmdf "._point" "_non" s)
  (vla-delete fa)

  (vl-cmdf "._ucs" "_restore" "mucs")
  (vl-cmdf "._ucs" "_delete" "mucs")
  (setvar 'cmdecho cmd)
  (vla-EndUndoMark (vla-get-activedocument (vlax-get-acad-object)))
  (princ)
)
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

ScottMC

  • Newt
  • Posts: 191
Re: 3dsolid Face Midpoint
« Reply #12 on: June 18, 2020, 11:26:48 AM »
Still the "mucs" is wrenching the code..

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: 3dsolid Face Midpoint
« Reply #13 on: June 18, 2020, 02:17:13 PM »
Still the "mucs" is wrenching the code..

Hi ScottMC - Sorry I don't know that to tell you - this works for me but it may have to do with "Command-s" in the error handler. I am running AutoCAD 2019 if you are running a much older version of AutoCAD prior to "Command-s" being added, then it wouldn't work in the error handler.

The only thing I can suggest is to add:
Code: [Select]
(if (tblsearch "UCS" "mucs")(vl-cmdf "._ucs" "_delete" "mucs"))before the (vl-cmdf "._ucs" "_save" "mucs") in the main code to eliminate any previously left over "mucs".

Then remove this code from the nested *error* sub-function:
Code: [Select]
     (if (tblsearch "UCS" "mucs")
       (progn (command-s "._ucs" "_restore" "mucs")(command-s "._ucs" "_delete" "mucs"))
     )

Doing UCS naming using vla functions or DXF is a challenge and although I tried - I was not able to delete the "mucs" UCS after setting it active (which works with the command). I will research it more when I have time. I may have to rename it to "unamed" first?

I hope this helps.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt