Notifications
Clear all

[Closed] Create s box from three points

Hai

how can be placed a box on surface ?
I ryed something like this , but is no realy ok :curious:


	
	fn getScreenRayAtNode obj =
    (
        screenRay = mapScreenToWorldRay mouse.pos
        finalRay = intersectRay obj screenRay
        finalRay
    )
	fn notUndefined arr = 
	(
		for i in arr where i == undefined do return false
		return true
	)
	fn get3PointsAngle A B C =
	(
		local V1 = B - A
		local V2 = B - C
		return acos (dot (normalize V1) (normalize V2)) -- angle
	)
	fn createBox p1 p2 p3 =
	(
		local p = (p1 + p2 + p3)/3
		local d = get3PointsAngle p1 p2 p3
		local l = distance p2 p3
		local w = distance p1 p2
		local h = 1
		local b = box pos:p length:l width:w height:h --dir:d
		b.rotation.angle = d
	)
	fn drawLineBetweenTwoPoints pointA pointB clr:green =
	(
		local sp = SplineShape pos:pointA
		addNewSpline sp
		addKnot sp 1 #corner #line PointA
		addKnot sp 1 #corner #line PointB
		updateShape sp
		sp.wirecolor = clr
		return sp
	)	
	
	tool three_point_box
	(
		--local key, fill, back, targ
		local sp = #(), p1, p2, p3, new_box
		local poly_obj = (selection as array)[1]
		on start do if classOf poly_obj != editable_poly do #stop
		on mousePoint click do --coordsys grid
		(
			local ir = getScreenRayAtNode poly_obj
			case click of
			(
				1: 
				(

				)
				2: 
				(	
					if ir != undefined do p1 = ir.pos
					if p1 != undefined do sp += #(point pos:p1 wirecolor:yellow)
				)
				3:
				(
					if ir != undefined do p2 = ir.pos
					if p2 != undefined do sp += #(point pos:p2 wirecolor:yellow)
					if notUndefined #(p1,p2) do sp += #(drawLineBetweenTwoPoints p1 p2 clr:green)
				)
				4:
				(
					if ir != undefined do p3 = ir.pos
					if p3 != undefined do sp += #(point pos:p3 wirecolor:yellow)
					if notUndefined #(p2,p3) do sp += #(drawLineBetweenTwoPoints p2 p3 clr:green)
					if notUndefined #(p1,p2,p3) do new_box = createBox p1 p2 p3
				)
				5:
				(
					if new_box != undefined do 
					(
						--resize and delete
 
						try (delete sp) catch()
						#stop
					)
				)
			)
		)
		on stop do try (delete sp) catch()
	)
	starttool three_point_box
)

1 Reply

I tryed a diferent way to do this:
Need some adjustments , but almost done


(	
	fn getScreenRayAtNode obj =
    (
        screenRay = mapScreenToWorldRay mouse.pos
        finalRay = intersectRay obj screenRay
        finalRay
    )
	fn drawLineBetweenTwoPoints pointA pointB clr:green =
	(
		local sp = SplineShape pos:pointA
		addNewSpline sp
		addKnot sp 1 #corner #line PointA
		addKnot sp 1 #corner #line PointB
		updateShape sp
		sp.wirecolor = clr
		return sp
	)	
	fn moveFace obj f p1 p2 p3 p4 dist =
	(
		local face_n = polyOp.getFaceNormal obj f
		local verts  = polyOp.getVertsUsingFace obj f as array
		polyOp.setVert obj verts[1] (p1+face_n*dist)
		polyOp.setVert obj verts[2] (p2+face_n*dist)
		polyOp.setVert obj verts[3] (p3+face_n*dist)
		polyOp.setVert obj verts[4] (p4+face_n*dist)
		redrawViews()
	)
	fn rotatePivotByNormal obj n =
	(
		local p = point pos:obj.pos dir:n
		local p_rotation = p.rotation
		obj.dir              = n
		obj.objectOffsetRot *= p_rotation
		obj.objectOffsetPos *= p_rotation
		delete p
	)
	tool three_point_box
	(
		--local key, fill, back, targ
		local sp = #(), p1, p2, p3, p4, new_box, verts =#(), last_click, start_new = true
		local poly_obj = (selection as array)[1]
		on start            do (if classOf poly_obj != editable_poly do #stop)
		on mousePoint click do --coordsys grid
		(
			local ir = getScreenRayAtNode poly_obj
			
			if ir != undefined and click > 1 do 
			(
				if start_new do
				(
					convertToPoly (new_box = mesh vertices:#() faces:#())
					new_box.update
					start_new = false
				)
				
				(
					if p1 == undefined then				
					(
						p1 = ir.pos
						verts += #(polyOp.createVert new_box p1)
						sp    += #(point pos:p1 wirecolor:white)
					)
					else if p2 == undefined then 
					(
						p2 = ir.pos
						verts += #(polyOp.createVert new_box p2)
						sp    += #(point pos:p2 wirecolor:white)
						sp    += #(drawLineBetweenTwoPoints p1 p2 clr:yellow)
					)
					else if p3 == undefined then 
					(
						p3 = ir.pos
						verts += #(polyOp.createVert new_box p3)
						--move second point in to right angle between p1 and p3
						local v1 = normalize (p2 - p1)
						local v2 = (p3 - p1)
						p2 = p1+v1*(dot v1 v2)
						polyOp.setVert new_box 2 p2 --move second point in to new pos
						--cleate fourth point
						p4 = p1 + (p3 - p2)
						verts += #(polyOp.createVert new_box p4)
						sp    += #(point pos:p3 wirecolor:white)
						sp    += #(drawLineBetweenTwoPoints p2 p3 clr:yellow)
						--create a face
						polyOp.createPolygon new_box verts
						--position and aligh pivot to object
						--new_box.pivot = (p1+p2+p3+p4)/4
						--rotatePivotByNormal new_box (polyOp.getFaceNormal new_box 1)
						--extrude face
						polyOp.extrudeFaces new_box 1 5
						last_click = mouse.pos
						--cap bottom of box
				
					)
					else 
					(
						last_click = undefined
						--reset and start from begin
						p1 = undefined
						p2 = undefined
						p3 = undefined
						start_new = true
						verts = #()
					)
				)
			)
		)
		on mouseMove click do 
		(
			if last_click != undefined do 
			(
				local dist = distance last_click mouse.pos
				--format "mouse dist:%
" dist
				moveFace new_box 1 p1 p2 p3 p4 (dist/10)
			)
		)
		on stop do try (delete sp) catch()
		on mouseAbort click do (print "aborted")
	)
	starttool three_point_box
)