import PyRx as Rx
import PyGe as Ge
import PyGi as Gi
import PyDb as Db
import PyAp as Ap
import PyEd as Ed
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
def PyRxCmd_pydoit():
try:
filter = [(Db.DxfCode.kDxfStart, "LINE,LWPOLYLINE")]
ssres = Ed.Editor.select(filter)
if ssres[0] != Ed.PromptStatus.eNormal:
return
points = []
ss = ssres[1].toList()
for id in ss:
if id.isDerivedFrom(Db.Curve.desc()):
curve = Db.Curve(id)
points.append(curve.getStartPoint())
points.append(curve.getEndPoint())
manager = pywrapcp.RoutingIndexManager(len(points) , 1, 0)
routing = pywrapcp.RoutingModel(manager)
def distance_callback(from_index, to_index):
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
l = points[from_node]
r = points[to_node]
return int(l.distanceTo(r) * 100)
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
solution = routing.SolveWithParameters(search_parameters)
path = []
index = routing.Start(0)
while not routing.IsEnd(index):
path.append(points[manager.IndexToNode(index)])
index = solution.Value(routing.NextVar(index))
pathlen = len(path)
pline = Db.Polyline(pathlen)
for ind in range(0,pathlen):
p = path[ind]
pline.addVertexAt(ind, Ge.Point2d(p.x,p.y))
pline.setClosed(True)
pline.setColorIndex(1)
db = Db.HostApplicationServices().workingDatabase()
model = Db.BlockTableRecord(db.modelSpaceId(), Db.OpenMode.ForWrite)
model.appendAcDbEntity(pline)
except Exception as err:
print(err)