I could use some help on this code. What I'm trying to achieve is:
When you've got multiple unclosed curves, you can select a couple of endpoints (or just two entire curves), and the script should find the two selected endpoints closest to each other and weld them together.
So basically, it should weld the two closest loose selected endpoints.
So far I've got this code, and it works correctly in some cases, but in other cases it welds the points completely wrong and I can't seem to figure out why. I think the mistake has something to do with the "attachSeg" variable that I'm not getting right, but I'm kind of lost... If you try it with a couple of random unclosed curves you'll see it immediately.
Code: Select all
local mesh = moho:DrawingMesh()
moho.document:PrepUndo(moho.drawingLayer)
moho.document:SetDirty()
-- * Find end points:
local endpointIDs = {}
local endpoints = {}
for i = 0, mesh:CountPoints() - 1 do
local pt = mesh:Point(i)
if (pt.fSelected) then
if pt:IsEndpoint() then
table.insert(endpointIDs, i)
table.insert(endpoints, pt)
end
end
end
--
local shortestDistance = 9999999
local movingPointID
local solidPointID
local movingPoint
local solidPoint
for i = 1, #endpoints do
local firstEndPoint = endpoints[i]
for j = 1, #endpoints do
local otherEndPoint = endpoints[j]
if firstEndPoint ~= otherEndPoint then
if firstEndPoint:Curve(0) ~= otherEndPoint:Curve(0) or #endpoints == 2 then
local distance = FO_Utilities:Distance(moho, firstEndPoint.fPos, otherEndPoint.fPos)
if distance < shortestDistance then
shortestDistance = distance
movingPoint = firstEndPoint
solidPoint = otherEndPoint
movingPointID = mesh:PointID(firstEndPoint)
solidPointID = mesh:PointID(otherEndPoint)
end
end
end
end
end
local pos = mesh:Point(movingPointID).fPos
-- * Find curve to attach copied point to:
local movingCurveID
local solidCurveID
for i = 0, mesh:CountCurves() - 1 do
local curve = mesh:Curve(i)
local curveID = mesh:CurveID(curve)
if movingPoint:IsPointOnCurve(curveID) then
movingCurveID = curveID
end
if solidPoint:IsPointOnCurve(curveID) then
solidCurveID = curveID
end
end
-- * Find curve and edge to attach new point to: TODO is this part wrong???
local attachCurveID
local attachSeg
attachCurveID, attachSeg = solidPoint:GetEndpointEdge(attachCurveID, attachSeg)
attachSeg = attachSeg + 1
-- * Add new point:
mesh:AddPoint(pos, attachCurveID, attachSeg, 0) -- * M_Mesh:AddPoint(pos, attachCurveID, attachSeg, frame, correctBezierHandles, preserveCorners)
local newPoint = mesh:Point(mesh:CountPoints()-1)
local newPointID = mesh:PointID(newPoint)
-- * Weld points:
print ("___welding:___")
print ("- movingPoint: "..movingPointID.." on curve "..movingCurveID)
print ("- solidPoint: "..solidPointID.." on curve "..solidCurveID)
print ("* newPoint: "..newPointID.." on curve "..attachCurveID.." (segment "..attachSeg..")")
mesh:WeldPoints(movingPointID, newPointID, 0)
-- *** Peak corners:
-- * Peak new point:
newPoint:SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
newPoint:ResetControlHandles(moho.drawingLayerFrame)
-- * Peak existing end point:
solidPoint:SetCurvature(MOHO.PEAKED, moho.drawingLayerFrame)
solidPoint:ResetControlHandles(moho.drawingLayerFrame)