Punto de restauración.
This commit is contained in:
397
Electrical/PowerConverter/PowerConverter.py
Normal file
397
Electrical/PowerConverter/PowerConverter.py
Normal file
@@ -0,0 +1,397 @@
|
||||
# /**********************************************************************
|
||||
# * *
|
||||
# * Copyright (c) 2021 Javier Braña <javier.branagutierrez@gmail.com> *
|
||||
# * *
|
||||
# * This program is free software; you can redistribute it and/or modify*
|
||||
# * it under the terms of the GNU Lesser General Public License (LGPL) *
|
||||
# * as published by the Free Software Foundation; either version 2 of *
|
||||
# * the License, or (at your option) any later version. *
|
||||
# * for detail see the LICENCE text file. *
|
||||
# * *
|
||||
# * This program is distributed in the hope that it will be useful, *
|
||||
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
# * GNU Library General Public License for more details. *
|
||||
# * *
|
||||
# * You should have received a copy of the GNU Library General Public *
|
||||
# * License along with this program; if not, write to the Free Software *
|
||||
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307*
|
||||
# * USA *
|
||||
# * *
|
||||
# ***********************************************************************
|
||||
|
||||
import FreeCAD
|
||||
import ArchComponent
|
||||
import os
|
||||
import zipfile
|
||||
import re
|
||||
|
||||
if FreeCAD.GuiUp:
|
||||
import FreeCADGui
|
||||
from DraftTools import translate
|
||||
else:
|
||||
# \cond
|
||||
def translate(ctxt,txt):
|
||||
return txt
|
||||
def QT_TRANSLATE_NOOP(ctxt,txt):
|
||||
return txt
|
||||
# \endcond
|
||||
|
||||
import os
|
||||
from PVPlantResources import DirIcons as DirIcons
|
||||
|
||||
__title__ = "PVPlant Areas"
|
||||
__author__ = "Javier Braña"
|
||||
__url__ = "http://www.sogos-solar.com"
|
||||
|
||||
import PVPlantResources
|
||||
from PVPlantResources import DirIcons as DirIcons
|
||||
Dir3dObjects = os.path.join(PVPlantResources.DirResources, "3dObjects")
|
||||
|
||||
|
||||
def makePCS():
|
||||
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "StringInverter")
|
||||
PowerConverter(obj)
|
||||
ViewProviderStringInverter(obj.ViewObject)
|
||||
|
||||
try:
|
||||
folder = FreeCAD.ActiveDocument.StringInverters
|
||||
except:
|
||||
folder = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", 'StringInverters')
|
||||
folder.Label = "StringInverters"
|
||||
folder.addObject(obj)
|
||||
return obj
|
||||
|
||||
|
||||
class PowerConverter(ArchComponent.Component):
|
||||
def __init__(self, obj):
|
||||
''' Initialize the Area object '''
|
||||
ArchComponent.Component.__init__(self, obj)
|
||||
|
||||
self.oldMPPTs = 0
|
||||
|
||||
self.Type = None
|
||||
self.obj = None
|
||||
self.setProperties(obj)
|
||||
|
||||
def setProperties(self, obj):
|
||||
pl = obj.PropertiesList
|
||||
|
||||
if not "File" in pl:
|
||||
obj.addProperty("App::PropertyFile",
|
||||
"File",
|
||||
"Inverter",
|
||||
"The base file this component is built upon")
|
||||
|
||||
if not ("MPPTs" in pl):
|
||||
obj.addProperty("App::PropertyQuantity",
|
||||
"MPPTs",
|
||||
"Inverter",
|
||||
"Points that define the area"
|
||||
).MPPTs = 0
|
||||
|
||||
if not ("Generator" in pl):
|
||||
obj.addProperty("App::PropertyEnumeration",
|
||||
"Generator",
|
||||
"Inverter",
|
||||
"Points that define the area"
|
||||
).Generator = ["Generic", "Library"]
|
||||
obj.Generator = "Generic"
|
||||
|
||||
if not ("Type" in pl):
|
||||
obj.addProperty("App::PropertyString",
|
||||
"Type",
|
||||
"Base",
|
||||
"Points that define the area"
|
||||
).Type = "PowerConverter"
|
||||
obj.setEditorMode("Type", 1)
|
||||
|
||||
self.Type = obj.Type
|
||||
obj.Proxy = self
|
||||
|
||||
def onDocumentRestored(self, obj):
|
||||
""" Method run when the document is restored """
|
||||
self.setProperties(obj)
|
||||
|
||||
def onBeforeChange(self, obj, prop):
|
||||
|
||||
if prop == "MPPTs":
|
||||
self.oldMPPTs = int(obj.MPPTs)
|
||||
|
||||
def onChanged(self, obj, prop):
|
||||
''' '''
|
||||
|
||||
if prop == "Generator":
|
||||
if obj.Generator == "Generic":
|
||||
obj.setEditorMode("MPPTs", 0)
|
||||
else:
|
||||
obj.setEditorMode("MPPTs", 1)
|
||||
|
||||
if prop == "MPPTs":
|
||||
''' '''
|
||||
if self.oldMPPTs > obj.MPPTs:
|
||||
''' borrar sobrantes '''
|
||||
obj.removeProperty()
|
||||
|
||||
elif self.oldMPPTs < obj.MPPTs:
|
||||
''' crear los faltantes '''
|
||||
for i in range(self.oldMPPTs, int(obj.MPPTs)):
|
||||
''' '''
|
||||
print(i)
|
||||
else:
|
||||
pass
|
||||
|
||||
if (prop == "File") and obj.File:
|
||||
''' '''
|
||||
|
||||
def execute(self, obj):
|
||||
''' '''
|
||||
# obj.Shape: compound
|
||||
# |- body: compound
|
||||
# |-- inverter: solid
|
||||
# |-- door: solid
|
||||
# |-- holder: solid
|
||||
|
||||
# |- connectors: compound
|
||||
# |-- DC: compound
|
||||
# |--- MPPT 1..x: compound
|
||||
# |---- positive: compound
|
||||
# |----- connector 1..y: ??
|
||||
# |---- negative 1..y: compound
|
||||
# |----- connector 1..y: ??
|
||||
# |-- AC: compound
|
||||
# |--- R,S,T,: ??
|
||||
# |-- Communication
|
||||
|
||||
pl = obj.Placement
|
||||
filename = self.getFile(obj)
|
||||
if filename:
|
||||
parts = self.getPartsList(obj)
|
||||
if parts:
|
||||
zdoc = zipfile.ZipFile(filename)
|
||||
if zdoc:
|
||||
f = zdoc.open(parts[list(parts.keys())[-1]][1])
|
||||
shapedata = f.read()
|
||||
f.close()
|
||||
shapedata = shapedata.decode("utf8")
|
||||
shape = self.cleanShape(shapedata, obj, parts[list(parts.keys())[-1]][2])
|
||||
obj.Shape = shape
|
||||
if not pl.isIdentity():
|
||||
obj.Placement = pl
|
||||
obj.MPPTs = len(shape.SubShapes[1].SubShapes[0].SubShapes)
|
||||
|
||||
def cleanShape(self, shapedata, obj, materials):
|
||||
"cleans the imported shape"
|
||||
|
||||
import Part
|
||||
shape = Part.Shape()
|
||||
shape.importBrepFromString(shapedata)
|
||||
'''if obj.FuseArch and materials:
|
||||
# separate lone edges
|
||||
shapes = []
|
||||
for edge in shape.Edges:
|
||||
found = False
|
||||
for solid in shape.Solids:
|
||||
for soledge in solid.Edges:
|
||||
if edge.hashCode() == soledge.hashCode():
|
||||
found = True
|
||||
break
|
||||
if found:
|
||||
break
|
||||
if found:
|
||||
break
|
||||
else:
|
||||
shapes.append(edge)
|
||||
print("solids:",len(shape.Solids),"mattable:",materials)
|
||||
for key,solindexes in materials.items():
|
||||
if key == "Undefined":
|
||||
# do not join objects with no defined material
|
||||
for solindex in [int(i) for i in solindexes.split(",")]:
|
||||
shapes.append(shape.Solids[solindex])
|
||||
else:
|
||||
fusion = None
|
||||
for solindex in [int(i) for i in solindexes.split(",")]:
|
||||
if not fusion:
|
||||
fusion = shape.Solids[solindex]
|
||||
else:
|
||||
fusion = fusion.fuse(shape.Solids[solindex])
|
||||
if fusion:
|
||||
shapes.append(fusion)
|
||||
shape = Part.makeCompound(shapes)
|
||||
try:
|
||||
shape = shape.removeSplitter()
|
||||
except Exception:
|
||||
print(obj.Label,": error removing splitter")'''
|
||||
return shape
|
||||
|
||||
def getFile(self, obj, filename=None):
|
||||
"gets a valid file, if possible"
|
||||
|
||||
if not filename:
|
||||
filename = obj.File
|
||||
if not filename:
|
||||
return None
|
||||
if not filename.lower().endswith(".fcstd"):
|
||||
return None
|
||||
if not os.path.exists(filename):
|
||||
# search for the file in the current directory if not found
|
||||
basename = os.path.basename(filename)
|
||||
currentdir = os.path.dirname(obj.Document.FileName)
|
||||
altfile = os.path.join(currentdir,basename)
|
||||
if altfile == obj.Document.FileName:
|
||||
return None
|
||||
elif os.path.exists(altfile):
|
||||
return altfile
|
||||
else:
|
||||
# search for subpaths in current folder
|
||||
altfile = None
|
||||
subdirs = self.splitall(os.path.dirname(filename))
|
||||
for i in range(len(subdirs)):
|
||||
subpath = [currentdir]+subdirs[-i:]+[basename]
|
||||
altfile = os.path.join(*subpath)
|
||||
if os.path.exists(altfile):
|
||||
return altfile
|
||||
return None
|
||||
return filename
|
||||
|
||||
def getPartsList(self, obj, filename=None):
|
||||
|
||||
"returns a list of Part-based objects in a FCStd file"
|
||||
|
||||
parts = {}
|
||||
materials = {}
|
||||
filename = self.getFile(obj,filename)
|
||||
if not filename:
|
||||
return parts
|
||||
zdoc = zipfile.ZipFile(filename)
|
||||
with zdoc.open("Document.xml") as docf:
|
||||
name = None
|
||||
label = None
|
||||
part = None
|
||||
materials = {}
|
||||
writemode = False
|
||||
for line in docf:
|
||||
line = line.decode("utf8")
|
||||
if "<Object name=" in line:
|
||||
n = re.findall('name=\"(.*?)\"',line)
|
||||
if n:
|
||||
name = n[0]
|
||||
elif "<Property name=\"Label\"" in line:
|
||||
writemode = True
|
||||
elif writemode and "<String value=" in line:
|
||||
n = re.findall('value=\"(.*?)\"',line)
|
||||
if n:
|
||||
label = n[0]
|
||||
writemode = False
|
||||
elif "<Property name=\"Shape\" type=\"Part::PropertyPartShape\"" in line:
|
||||
writemode = True
|
||||
elif writemode and "<Part file=" in line:
|
||||
n = re.findall('file=\"(.*?)\"',line)
|
||||
if n:
|
||||
part = n[0]
|
||||
writemode = False
|
||||
elif "<Property name=\"MaterialsTable\" type=\"App::PropertyMap\"" in line:
|
||||
writemode = True
|
||||
elif writemode and "<Item key=" in line:
|
||||
n = re.findall('key=\"(.*?)\"',line)
|
||||
v = re.findall('value=\"(.*?)\"',line)
|
||||
if n and v:
|
||||
materials[n[0]] = v[0]
|
||||
elif writemode and "</Map>" in line:
|
||||
writemode = False
|
||||
elif "</Object>" in line:
|
||||
if name and label and part:
|
||||
parts[name] = [label,part,materials]
|
||||
name = None
|
||||
label = None
|
||||
part = None
|
||||
materials = {}
|
||||
writemode = False
|
||||
return parts
|
||||
|
||||
def getColors(self,obj):
|
||||
|
||||
"returns the DiffuseColor of the referenced object"
|
||||
|
||||
filename = self.getFile(obj)
|
||||
if not filename:
|
||||
return None
|
||||
part = obj.Part
|
||||
if not obj.Part:
|
||||
return None
|
||||
zdoc = zipfile.ZipFile(filename)
|
||||
if not "GuiDocument.xml" in zdoc.namelist():
|
||||
return None
|
||||
colorfile = None
|
||||
with zdoc.open("GuiDocument.xml") as docf:
|
||||
writemode1 = False
|
||||
writemode2 = False
|
||||
for line in docf:
|
||||
line = line.decode("utf8")
|
||||
if ("<ViewProvider name=" in line) and (part in line):
|
||||
writemode1 = True
|
||||
elif writemode1 and ("<Property name=\"DiffuseColor\"" in line):
|
||||
writemode1 = False
|
||||
writemode2 = True
|
||||
elif writemode2 and ("<ColorList file=" in line):
|
||||
n = re.findall('file=\"(.*?)\"',line)
|
||||
if n:
|
||||
colorfile = n[0]
|
||||
break
|
||||
if not colorfile:
|
||||
return None
|
||||
if not colorfile in zdoc.namelist():
|
||||
return None
|
||||
colors = []
|
||||
cf = zdoc.open(colorfile)
|
||||
buf = cf.read()
|
||||
cf.close()
|
||||
for i in range(1,int(len(buf)/4)):
|
||||
colors.append((buf[i*4+3]/255.0,buf[i*4+2]/255.0,buf[i*4+1]/255.0,buf[i*4]/255.0))
|
||||
if colors:
|
||||
return colors
|
||||
return None
|
||||
|
||||
def splitall(self,path):
|
||||
|
||||
"splits a path between its components"
|
||||
|
||||
allparts = []
|
||||
while 1:
|
||||
parts = os.path.split(path)
|
||||
if parts[0] == path: # sentinel for absolute paths
|
||||
allparts.insert(0, parts[0])
|
||||
break
|
||||
elif parts[1] == path: # sentinel for relative paths
|
||||
allparts.insert(0, parts[1])
|
||||
break
|
||||
else:
|
||||
path = parts[0]
|
||||
allparts.insert(0, parts[1])
|
||||
return allparts
|
||||
|
||||
class ViewProviderStringInverter(ArchComponent.ViewProviderComponent):
|
||||
def __init__(self, vobj):
|
||||
ArchComponent.ViewProviderComponent.__init__(self, vobj)
|
||||
|
||||
def getIcon(self):
|
||||
return str(os.path.join(PVPlantResources.DirIcons, "Inverter.svg"))
|
||||
|
||||
class CommandPowerConverter:
|
||||
|
||||
def GetResources(self):
|
||||
return {'Pixmap': str(os.path.join(PVPlantResources.DirIcons, "Inverter.svg")),
|
||||
'Accel': "E, I",
|
||||
'MenuText': "String Inverter",
|
||||
'ToolTip': "String Placement",}
|
||||
|
||||
def Activated(self):
|
||||
sinverter = makeStringInverter()
|
||||
|
||||
def IsActive(self):
|
||||
active = not (FreeCAD.ActiveDocument is None)
|
||||
return active
|
||||
|
||||
if FreeCAD.GuiUp:
|
||||
FreeCADGui.addCommand('PowerConverter', CommandPowerConverter())
|
||||
|
||||
387
Electrical/group.py
Normal file
387
Electrical/group.py
Normal file
@@ -0,0 +1,387 @@
|
||||
def groupTrackersToTransformers(transformer_power, max_distance):
|
||||
import numpy as np
|
||||
from scipy.spatial import KDTree
|
||||
import FreeCAD
|
||||
from collections import deque
|
||||
|
||||
# 1. Obtener todos los trackers válidos
|
||||
valid_trackers = []
|
||||
valid_points = []
|
||||
valid_power = []
|
||||
|
||||
for tracker in FreeCAD.ActiveDocument.Objects:
|
||||
if hasattr(tracker, 'Proxy') and (tracker.Proxy.Type == "Tracker"):
|
||||
base = tracker.Placement.Base
|
||||
if all(np.isfinite([base.x, base.y, base.z])):
|
||||
valid_trackers.append(tracker)
|
||||
valid_points.append([base.x, base.y])
|
||||
valid_power.append(tracker.Setup.TotalPower)
|
||||
|
||||
if not valid_trackers:
|
||||
FreeCAD.Console.PrintWarning("No se encontraron trackers válidos para agrupar\n")
|
||||
return
|
||||
|
||||
# 2. Obtener parámetros de los CTs
|
||||
target_power = transformer_power * 1.2
|
||||
points = np.array(valid_points)
|
||||
power_values = np.array(valid_power)
|
||||
|
||||
# 3. Determinar dirección de barrido (oeste a este por defecto)
|
||||
min_x, min_y = np.min(points, axis=0)
|
||||
max_x, max_y = np.max(points, axis=0)
|
||||
|
||||
# 4. Ordenar trackers de oeste a este (menor X a mayor X)
|
||||
'''
|
||||
Norte a Sur: sorted_indices = np.argsort(-points[:, 1]) (Y descendente)
|
||||
Sur a Norte: sorted_indices = np.argsort(points[:, 1]) (Y ascendente)
|
||||
Este a Oeste: sorted_indices = np.argsort(-points[:, 0]) (X descendente)
|
||||
'''
|
||||
sorted_indices = np.argsort(points[:, 0])
|
||||
sorted_points = points[sorted_indices]
|
||||
sorted_power = power_values[sorted_indices]
|
||||
sorted_trackers = [valid_trackers[i] for i in sorted_indices]
|
||||
|
||||
# 5. Crear KDTree para búsquedas rápidas
|
||||
kdtree = KDTree(sorted_points)
|
||||
|
||||
# 6. Algoritmo de barrido espacial
|
||||
transformer_groups = []
|
||||
used_indices = set()
|
||||
|
||||
# Función para expandir un grupo desde un punto inicial
|
||||
def expand_group(start_idx):
|
||||
group = []
|
||||
total_power = 0
|
||||
queue = deque([start_idx])
|
||||
|
||||
while queue and total_power < target_power:
|
||||
idx = queue.popleft()
|
||||
if idx in used_indices:
|
||||
continue
|
||||
|
||||
# Añadir tracker al grupo si no excede la potencia
|
||||
tracker_power = sorted_power[idx]
|
||||
if total_power + tracker_power > target_power * 1.05:
|
||||
continue
|
||||
|
||||
group.append(sorted_trackers[idx])
|
||||
total_power += tracker_power
|
||||
used_indices.add(idx)
|
||||
|
||||
# Buscar vecinos cercanos
|
||||
neighbors = kdtree.query_ball_point(
|
||||
sorted_points[idx],
|
||||
max_distance
|
||||
)
|
||||
|
||||
# Filtrar vecinos no usados y ordenar por proximidad al punto inicial
|
||||
neighbors = [n for n in neighbors if n not in used_indices]
|
||||
neighbors.sort(key=lambda n: abs(n - start_idx))
|
||||
queue.extend(neighbors)
|
||||
|
||||
return group, total_power
|
||||
|
||||
# 7. Barrido principal de oeste a este
|
||||
for i in range(len(sorted_points)):
|
||||
if i in used_indices:
|
||||
continue
|
||||
|
||||
group, total_power = expand_group(i)
|
||||
|
||||
if group:
|
||||
# Calcular centro del grupo
|
||||
group_points = np.array([t.Placement.Base[:2] for t in group])
|
||||
center = np.mean(group_points, axis=0)
|
||||
|
||||
transformer_groups.append({
|
||||
'trackers': group,
|
||||
'total_power': total_power,
|
||||
'center': center
|
||||
})
|
||||
|
||||
# 8. Manejar grupos residuales (si los hay)
|
||||
unused_indices = set(range(len(sorted_points))) - used_indices
|
||||
if unused_indices:
|
||||
# Intentar añadir trackers residuales a grupos existentes
|
||||
for idx in unused_indices:
|
||||
point = sorted_points[idx]
|
||||
tracker_power = sorted_power[idx]
|
||||
|
||||
# Buscar el grupo más cercano que pueda aceptar este tracker
|
||||
best_group = None
|
||||
min_distance = float('inf')
|
||||
|
||||
for group in transformer_groups:
|
||||
if group['total_power'] + tracker_power <= target_power * 1.05:
|
||||
dist = np.linalg.norm(point - group['center'])
|
||||
if dist < min_distance and dist < max_distance * 1.5:
|
||||
min_distance = dist
|
||||
best_group = group
|
||||
|
||||
# Añadir al grupo si se encontró uno adecuado
|
||||
if best_group:
|
||||
best_group['trackers'].append(sorted_trackers[idx])
|
||||
best_group['total_power'] += tracker_power
|
||||
# Actualizar centro del grupo
|
||||
group_points = np.array([t.Placement.Base[:2] for t in best_group['trackers']])
|
||||
best_group['center'] = np.mean(group_points, axis=0)
|
||||
else:
|
||||
# Crear un nuevo grupo con este tracker residual
|
||||
group = [sorted_trackers[idx]]
|
||||
center = point
|
||||
transformer_groups.append({
|
||||
'trackers': group,
|
||||
'total_power': tracker_power,
|
||||
'center': center
|
||||
})
|
||||
|
||||
# 9. Crear los grupos en FreeCAD
|
||||
transformer_group = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", "Transformers")
|
||||
transformer_group.Label = "Centros de Transformación"
|
||||
|
||||
for i, group in enumerate(transformer_groups):
|
||||
# Crear la esfera que representará el CT
|
||||
ct_sphere = FreeCAD.ActiveDocument.addObject("Part::Sphere", f"CT_{i + 1}")
|
||||
ct_sphere.Radius = 5000 # 2m de radio
|
||||
ct_sphere.Placement.Base = FreeCAD.Vector(group['center'][0], group['center'][1], 0)
|
||||
|
||||
# Añadir propiedades personalizadas
|
||||
ct_sphere.addProperty("App::PropertyLinkList", "Trackers", "CT",
|
||||
"Lista de trackers asociados a este CT")
|
||||
ct_sphere.addProperty("App::PropertyFloat", "TotalPower", "CT",
|
||||
"Potencia total del grupo (W)")
|
||||
ct_sphere.addProperty("App::PropertyFloat", "NominalPower", "CT",
|
||||
"Potencia nominal del transformador (W)")
|
||||
ct_sphere.addProperty("App::PropertyFloat", "Utilization", "CT",
|
||||
"Porcentaje de utilización (Total/Nominal)")
|
||||
|
||||
# Establecer valores de las propiedades
|
||||
ct_sphere.Trackers = group['trackers']
|
||||
ct_sphere.TotalPower = group['total_power'].Value
|
||||
ct_sphere.NominalPower = transformer_power
|
||||
ct_sphere.Utilization = (group['total_power'].Value / transformer_power) * 100
|
||||
|
||||
# Configurar visualización
|
||||
# Calcular color basado en utilización (verde < 100%, amarillo < 110%, rojo > 110%)
|
||||
utilization = ct_sphere.Utilization
|
||||
if utilization <= 100:
|
||||
color = (0.0, 1.0, 0.0) # Verde
|
||||
elif utilization <= 110:
|
||||
color = (1.0, 1.0, 0.0) # Amarillo
|
||||
else:
|
||||
color = (1.0, 0.0, 0.0) # Rojo
|
||||
|
||||
ct_sphere.ViewObject.ShapeColor = color
|
||||
ct_sphere.ViewObject.Transparency = 40 # 40% de transparencia
|
||||
|
||||
# Añadir etiqueta con información
|
||||
ct_sphere.ViewObject.DisplayMode = "Shaded"
|
||||
ct_sphere.Label = f"CT {i + 1} ({ct_sphere.TotalPower / 1000:.1f}kW/{ct_sphere.NominalPower / 1000:.1f}kW)"
|
||||
|
||||
# Añadir al grupo principal
|
||||
transformer_group.addObject(ct_sphere)
|
||||
|
||||
FreeCAD.Console.PrintMessage(f"Se crearon {len(transformer_groups)} centros de transformación\n")
|
||||
onSelectGatePoint()
|
||||
|
||||
|
||||
import FreeCAD, FreeCADGui, Part
|
||||
import numpy as np
|
||||
from scipy.stats import linregress
|
||||
from PySide import QtGui
|
||||
|
||||
class InternalPathCreator:
|
||||
def __init__(self, gate_point, strategy=1, path_width=4000):
|
||||
self.gate_point = gate_point
|
||||
self.strategy = strategy
|
||||
self.path_width = path_width
|
||||
self.ct_spheres = []
|
||||
self.ct_positions = []
|
||||
|
||||
def get_transformers(self):
|
||||
transformers_group = FreeCAD.ActiveDocument.getObject("Transformers")
|
||||
if not transformers_group:
|
||||
FreeCAD.Console.PrintError("No se encontró el grupo 'Transformers'\n")
|
||||
return False
|
||||
|
||||
self.ct_spheres = transformers_group.Group
|
||||
if not self.ct_spheres:
|
||||
FreeCAD.Console.PrintWarning("No hay Centros de Transformación en el grupo\n")
|
||||
return False
|
||||
|
||||
# Obtener las posiciones de los CTs
|
||||
for sphere in self.ct_spheres:
|
||||
base = sphere.Placement.Base
|
||||
self.ct_positions.append(FreeCAD.Vector(base.x, base.y, 0))
|
||||
return True
|
||||
|
||||
def create_paths(self):
|
||||
if not self.get_transformers():
|
||||
return []
|
||||
|
||||
if self.strategy == 1:
|
||||
return self.create_direct_paths()
|
||||
elif self.strategy == 2:
|
||||
return self.create_unified_path()
|
||||
else:
|
||||
FreeCAD.Console.PrintError("Estrategia no válida. Use 1 o 2.\n")
|
||||
return []
|
||||
|
||||
def create_direct_paths(self):
|
||||
"""Estrategia 1: Caminos independientes desde cada CT hasta la puerta"""
|
||||
paths = []
|
||||
for ct in self.ct_positions:
|
||||
paths.append([ct, self.gate_point])
|
||||
return paths
|
||||
|
||||
def create_unified_path(self):
|
||||
"""Estrategia 2: Único camino que une todos los CTs y la puerta usando regresión lineal"""
|
||||
if not self.ct_positions:
|
||||
return []
|
||||
|
||||
all_points = self.ct_positions + [self.gate_point]
|
||||
x = [p.x for p in all_points]
|
||||
y = [p.y for p in all_points]
|
||||
|
||||
# Manejar caso de puntos alineados verticalmente
|
||||
if np.std(x) < 1e-6:
|
||||
sorted_points = sorted(all_points, key=lambda p: p.y)
|
||||
paths = []
|
||||
for i in range(len(sorted_points) - 1):
|
||||
paths.append([sorted_points[i], sorted_points[i + 1]])
|
||||
return paths
|
||||
|
||||
# Calcular regresión lineal
|
||||
slope, intercept, _, _, _ = linregress(x, y)
|
||||
|
||||
# Función para proyectar puntos
|
||||
def project_point(point):
|
||||
x0, y0 = point.x, point.y
|
||||
if abs(slope) > 1e6:
|
||||
return FreeCAD.Vector(x0, intercept, 0)
|
||||
x_proj = (x0 + slope * (y0 - intercept)) / (1 + slope ** 2)
|
||||
y_proj = slope * x_proj + intercept
|
||||
return FreeCAD.Vector(x_proj, y_proj, 0)
|
||||
|
||||
projected_points = [project_point(p) for p in all_points]
|
||||
|
||||
# Calcular distancias a lo largo de la línea
|
||||
ref_point = projected_points[0]
|
||||
direction_vector = FreeCAD.Vector(1, slope).normalize()
|
||||
distances = []
|
||||
for p in projected_points:
|
||||
vec_to_point = p - ref_point
|
||||
distance = vec_to_point.dot(direction_vector)
|
||||
distances.append(distance)
|
||||
|
||||
# Ordenar por distancia
|
||||
sorted_indices = np.argsort(distances)
|
||||
sorted_points = [all_points[i] for i in sorted_indices]
|
||||
|
||||
# Crear caminos
|
||||
paths = []
|
||||
for i in range(len(sorted_points) - 1):
|
||||
paths.append([sorted_points[i], sorted_points[i + 1]])
|
||||
return paths
|
||||
|
||||
def create_3d_path(self, path_poly):
|
||||
"""Crea geometría 3D para el camino adaptada a orientación norte-sur"""
|
||||
segments = []
|
||||
for i in range(len(path_poly.Vertexes) - 1):
|
||||
start = path_poly.Vertexes[i].Point
|
||||
end = path_poly.Vertexes[i + 1].Point
|
||||
direction = end - start
|
||||
|
||||
# Determinar orientación predominante
|
||||
if abs(direction.x) > abs(direction.y):
|
||||
normal = FreeCAD.Vector(0, 1, 0) # Norte-sur
|
||||
else:
|
||||
normal = FreeCAD.Vector(1, 0, 0) # Este-oeste
|
||||
|
||||
offset = normal * self.path_width / 2
|
||||
|
||||
# Crear puntos para la sección transversal
|
||||
p1 = start + offset
|
||||
p2 = start - offset
|
||||
p3 = end - offset
|
||||
p4 = end + offset
|
||||
|
||||
# Crear cara
|
||||
wire = Part.makePolygon([p1, p2, p3, p4, p1])
|
||||
face = Part.Face(wire)
|
||||
segments.append(face)
|
||||
|
||||
if segments:
|
||||
'''road_shape = segments[0].fuse(segments[1:])
|
||||
return road_shape.removeSplitter()'''
|
||||
return Part.makeCompound(segments)
|
||||
return Part.Shape()
|
||||
|
||||
def build(self):
|
||||
paths = self.create_paths()
|
||||
if not paths:
|
||||
return
|
||||
|
||||
path_group = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", "InternalPaths")
|
||||
path_group.Label = f"Caminos Internos (Estrategia {self.strategy})"
|
||||
|
||||
for i, path in enumerate(paths):
|
||||
poly = Part.makePolygon(path)
|
||||
|
||||
# Objeto para la línea central
|
||||
path_obj = FreeCAD.ActiveDocument.addObject("Part::Feature", f"Path_{i + 1}")
|
||||
path_obj.Shape = poly
|
||||
path_obj.ViewObject.LineWidth = 3.0
|
||||
path_obj.ViewObject.LineColor = (0.0, 0.0, 1.0)
|
||||
|
||||
# Objeto para la superficie 3D
|
||||
road_shape = self.create_3d_path(poly)
|
||||
road_obj = FreeCAD.ActiveDocument.addObject("Part::Feature", f"Road_{i + 1}")
|
||||
road_obj.Shape = road_shape
|
||||
road_obj.ViewObject.ShapeColor = (0.7, 0.7, 0.7)
|
||||
|
||||
path_group.addObject(path_obj)
|
||||
path_group.addObject(road_obj)
|
||||
|
||||
FreeCAD.Console.PrintMessage(f"Se crearon {len(paths)} segmentos de caminos internos\n")
|
||||
|
||||
|
||||
# Función para mostrar el diálogo de estrategia
|
||||
def show_path_strategy_dialog(gate_point):
|
||||
dialog = QtGui.QDialog()
|
||||
dialog.setWindowTitle("Seleccionar Estrategia de Caminos")
|
||||
layout = QtGui.QVBoxLayout(dialog)
|
||||
|
||||
label = QtGui.QLabel("Seleccione la estrategia para crear los caminos internos:")
|
||||
layout.addWidget(label)
|
||||
|
||||
rb1 = QtGui.QRadioButton("Estrategia 1: Caminos independientes desde cada CT hasta la puerta")
|
||||
rb1.setChecked(True)
|
||||
layout.addWidget(rb1)
|
||||
|
||||
rb2 = QtGui.QRadioButton("Estrategia 2: Único camino que une todos los CTs y la puerta")
|
||||
layout.addWidget(rb2)
|
||||
|
||||
btn_box = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
|
||||
layout.addWidget(btn_box)
|
||||
|
||||
def on_accept():
|
||||
strategy = 1 if rb1.isChecked() else 2
|
||||
dialog.accept()
|
||||
creator = InternalPathCreator(gate_point, strategy)
|
||||
creator.build()
|
||||
|
||||
btn_box.accepted.connect(on_accept)
|
||||
btn_box.rejected.connect(dialog.reject)
|
||||
|
||||
dialog.exec_()
|
||||
|
||||
|
||||
# Uso: seleccionar un punto para la puerta de entrada
|
||||
def onSelectGatePoint():
|
||||
'''gate = FreeCAD.ActiveDocument.findObjects(Name="FenceGate")[0]
|
||||
gate_point = gate.Placement.Base
|
||||
show_path_strategy_dialog(gate_point)'''
|
||||
|
||||
sel = FreeCADGui.Selection.getSelectionEx()[0]
|
||||
show_path_strategy_dialog(sel.SubObjects[0].CenterOfMass)
|
||||
Reference in New Issue
Block a user