23 Commits

Author SHA1 Message Date
Javier Braña 25fd92e4f0 Limpieza: eliminados archivos viejos (PVPLantPlacement-old_2022.py, -copia, -old, .bak) 2026-05-04 13:35:21 +02:00
Javier Braña 5abd4fae02 Placement: limpieza masiva. Eliminadas clases _old y _new1 (~550 líneas). Eliminadas funciones globales (~600 líneas). Movidas a Civil/PVPlantPlacementCalc.py. PVPlantPlacement.py pasa de 2352→663 líneas solo con TaskPanels y comandos. 2026-05-04 01:14:00 +02:00
Javier Braña e461ab2e80 Platform: FeaturePython completo. Objeto Platform con SourceFrames, SlopeTolerance, Shape. EarthWorks acepta Platform o frames. ViewProvider verde semitransparente. 2026-05-04 00:09:09 +02:00
Javier Braña 1a22121f87 EarthWorks: separado en PVPlantPlatform (plataforma desde trackers) + PVPlantEarthWorks (solo volumen cut/fill). Platform reutilizable independientemente. 2026-05-03 23:52:58 +02:00
Javier Braña 2858b58d86 EarthWorks: rewrite completo. Limpio, modular, con función compute_earthworks() principal. Cut=rojo, Fill=azul. Eliminado código muerto (3 versiones conviviendo, accept() duplicada, viewprovider comentado) 2026-05-03 22:50:55 +02:00
Javier Braña f3f94d4f59 Road: sistema de alineamiento profesional. Alignment (eje+estaciones), Road multicapa con cubicación contra terreno 2026-05-03 21:43:18 +02:00
Javier Braña f4d43bedd0 Placement: getAligments con linspace, _calculate_placement progreso, accept simplificado, _get_or_create optimizado 2026-05-03 20:25:40 +02:00
Javier Braña a67001bb88 Placement: isInside optimizado con shapely + caché LRU + prefiltro BoundBox. Caché se limpia al cambiar área 2026-05-03 19:56:01 +02:00
Javier Braña 26311cb344 Placement: motor unificado _calculate_placement para aligned/non_aligned, misma lógica compartida 2026-05-03 19:10:49 +02:00
Javier Braña 6c2db07493 Placement: fix 7 excepts genéricos -> específicos (KeyError, Exception, Part.OCCError) 2026-05-03 13:23:48 +02:00
Javier Braña 7d1127c6b5 Trench: fix except genérico -> AttributeError 2026-05-03 02:57:50 +02:00
Javier Braña 7a54e424cb Georeferencing: fallback modo manual cuando QtWebEngine no está disponible (FreeCAD flatpak) 2026-05-03 01:15:16 +02:00
Javier Braña 065f840941 Georeferencing: import QWebEngineView multi-versión (PySide6 QtWebEngineCore/Quick fallback) 2026-05-03 00:56:01 +02:00
Javier Braña 74aedf6122 PVPlant: utm → pyproj (adaptador con sys.modules patch en ImportGrid, eliminado de requirements) 2026-05-03 00:32:36 +02:00
Javier Braña 7c81beb1ba PVPlant: PySide2 -> PySide genérico (FreeCAD resuelve el binding), eliminado de requirements 2026-05-03 00:22:53 +02:00
Javier Braña 02b639d4ed requirements: añadido pandas, separado rtree 2026-05-03 00:09:55 +02:00
Javier Braña fc4142cfec PVPlantTerrain: fix visualización en pantalla — updateData escuchaba Mesh en vez de mesh, añadido publishProperty forzado, más display modes 2026-05-02 23:49:48 +02:00
javier a515f31726 hydro/hydrological: fix except genérico -> (IndexError, AttributeError) 2026-05-02 23:34:53 +02:00
javier e0a0dc2f0d EarthWorks: fix except genérico -> Part.OCCError 2026-05-02 23:22:41 +02:00
javier 02d6c4f412 ImportGrid: fix str(e) sin except, excepts genéricos a específicos 2026-05-02 23:20:59 +02:00
javier e129aba2fe Site: fix computeAreas return prematuro, excepts genéricos a ImportError/Exception 2026-05-02 23:16:27 +02:00
javier 9d65323052 TerrainAnalisys: fix hardcode i=2, var obj undefined, remove threading innecesario 2026-05-02 22:50:10 +02:00
javier 0b13a8c5f1 Mejoras PVPlantTerrain: fix XYZ import, DEM rendimiento, ViewProvider boundary+contour, error handling 2026-05-02 22:47:58 +02:00
43 changed files with 5038 additions and 8745 deletions
+2
View File
@@ -0,0 +1,2 @@
__pycache__/
*.pyc
+387
View File
@@ -0,0 +1,387 @@
# /**********************************************************************
# * *
# * Copyright (c) 2026 Javier Braña <javier.branagutierrez@gmail.com> *
# * *
# * EarthWorks - Cálculo de movimiento de tierras *
# * *
# * Calcula volúmenes de desmonte (cut) y terraplén (fill) entre una *
# * plataforma diseñada (generada por PVPlantPlatform) y el terreno *
# * natural representado por un mesh. *
# * *
# * Flujo: *
# * 1. build_platform(frames) → Part.Solid (superficie diseñada) *
# * 2. cut_mesh = mesh_above(platform_mesh, terrain_mesh) *
# * 3. fill_mesh = mesh_below(platform_mesh, terrain_mesh) *
# * 4. Volumen = mesh.Volume *
# * *
# ***********************************************************************
import FreeCAD
import Part
import Mesh
import math
if FreeCAD.GuiUp:
import FreeCADGui, os
from PySide import QtCore, QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
else:
def translate(ctxt, txt): return txt
def QT_TRANSLATE_NOOP(ctxt, txt): return txt
import PVPlantResources
from PVPlantResources import DirIcons as DirIcons
from .PVPlantPlatform import build_platform, get_platform_shape, make_platform
VOLUME_TYPES = ["Fill", "Cut"]
def compute_earthworks(platform_or_frames, terrain_mesh, slope_tolerance=10.0):
"""
Calcula el movimiento de tierras.
Args:
platform_or_frames: Objeto Platform o lista de frames/trackers
Si es Platform, usa su Shape directamente.
Si es lista de frames, genera la plataforma primero.
terrain_mesh: Mesh del terreno natural
slope_tolerance: pendiente máxima E-W (grados)
Returns:
tuple: (mesh_cut, mesh_fill, volume_cut_mm3, volume_fill_mm3)
"""
import MeshPart
# 1. Obtener la plataforma
if hasattr(platform_or_frames, 'Proxy') and hasattr(platform_or_frames.Proxy, '__class__'):
cls_name = platform_or_frames.Proxy.__class__.__name__
if cls_name == 'Platform':
# Ya es un objeto Platform → usar su Shape
platform = get_platform_shape(platform_or_frames)
else:
platform = build_platform([platform_or_frames], slope_tolerance)
elif hasattr(platform_or_frames, '__iter__'):
# Es una lista de frames
platform = build_platform(platform_or_frames, slope_tolerance)
else:
platform = None
# 2. Convertir plataforma a mesh para booleanos
try:
platform_mesh = MeshPart.meshFromShape(
Shape=platform,
LinearDeflection=500,
AngularDeflection=0.5
)
except Exception as e:
FreeCAD.Console.PrintError(f"Error al meshificar la plataforma: {e}\n")
return None, None, 0, 0
if platform_mesh is None or platform_mesh.countPoints() == 0:
return None, None, 0, 0
# 3. Calcular corte y relleno
cut_mesh = _mesh_above(platform_mesh, terrain_mesh)
fill_mesh = _mesh_below(platform_mesh, terrain_mesh)
volume_cut = _mesh_volume(cut_mesh)
volume_fill = _mesh_volume(fill_mesh)
return cut_mesh, fill_mesh, volume_cut, volume_fill
def _mesh_above(reference, terrain):
"""
Porción del mesh de referencia que está por encima del terreno.
Representa material a excavar (cut).
"""
try:
common = reference.common(terrain)
if common and common.countPoints() > 3:
return common
except Exception:
pass
return None
def _mesh_below(reference, terrain):
"""
Porción del mesh de referencia que está por debajo del terreno.
Representa material a rellenar (fill).
"""
try:
diff = reference.cut(terrain)
if diff and diff.countPoints() > 3:
return diff
except Exception:
pass
return None
def _mesh_volume(mesh):
"""Volumen de un mesh en mm³. Retorna 0 si no hay mesh válido."""
if mesh is None or mesh.countPoints() < 4:
return 0
try:
return mesh.Volume
except Exception:
return 0
def makeEarthWorksVolume(vtype=0):
"""Crea un objeto FeaturePython con el mesh de volumen."""
obj = FreeCAD.ActiveDocument.addObject(
"Part::FeaturePython", VOLUME_TYPES[vtype])
EarthWorksVolume(obj)
ViewProviderEarthWorksVolume(obj.ViewObject)
return obj
# =========================================================================
# FeaturePython: EarthWorksVolume
# =========================================================================
class EarthWorksVolume:
"""Objeto que almacena un mesh de volumen (cut o fill)."""
def __init__(self, obj):
self.setProperties(obj)
obj.Proxy = self
def setProperties(self, obj):
pl = obj.PropertiesList
if "VolumeType" not in pl:
obj.addProperty(
"App::PropertyEnumeration", "VolumeType", "Volume",
"Fill o Cut").VolumeType = VOLUME_TYPES
if "VolumeMesh" not in pl:
obj.addProperty(
"Mesh::PropertyMeshKernel", "VolumeMesh", "Volume",
"Mesh del volumen")
obj.setEditorMode("VolumeMesh", 2)
if "Volume" not in pl:
obj.addProperty(
"App::PropertyVolume", "Volume", "Volume",
"Volumen calculado (mm³)")
obj.setEditorMode("Volume", 1)
obj.IfcType = "Civil Element"
obj.setEditorMode("IfcType", 1)
def onDocumentRestored(self, obj):
self.setProperties(obj)
def onChange(self, obj, prop):
if prop == "VolumeMesh" and obj.VolumeMesh:
obj.Volume = obj.VolumeMesh.Volume
def execute(self, obj):
pass
def __getstate__(self):
return None
def __setstate__(self, state):
return None
# =========================================================================
# ViewProvider (Coin3D)
# =========================================================================
class ViewProviderEarthWorksVolume:
def __init__(self, vobj):
pl = vobj.PropertiesList
is_cut = vobj.Object.VolumeType == "Cut"
r, g, b = (1.0, 0.0, 0.0) if is_cut else (0.0, 0.0, 1.0)
if "Transparency" not in pl:
vobj.addProperty(
"App::PropertyIntegerConstraint", "Transparency",
"Surface Style", "Transparencia (0=opaco, 100=invisible)")
vobj.Transparency = (50, 0, 100, 1)
if "ShapeColor" not in pl:
vobj.addProperty(
"App::PropertyColor", "ShapeColor", "Surface Style",
"Color de superficie")
vobj.ShapeColor = (r, g, b, vobj.Transparency / 100)
if "ShapeMaterial" not in pl:
vobj.addProperty(
"App::PropertyMaterial", "ShapeMaterial", "Surface Style",
"Material de superficie")
vobj.ShapeMaterial = FreeCAD.Material()
vobj.Proxy = self
vobj.ShapeMaterial.DiffuseColor = vobj.ShapeColor
def onChanged(self, vobj, prop):
if prop in ("ShapeColor", "Transparency"):
if hasattr(vobj, "ShapeColor") and hasattr(vobj, "Transparency"):
c = vobj.ShapeColor
t = vobj.Transparency
vobj.ShapeMaterial.DiffuseColor = (c[0], c[1], c[2], t / 100)
if prop == "ShapeMaterial":
if hasattr(self, "face_material"):
mat = vobj.ShapeMaterial
self.face_material.diffuseColor.setValue(mat.DiffuseColor[:3])
self.face_material.transparency = mat.DiffuseColor[3]
def attach(self, vobj):
from pivy import coin
self.geo_coords = coin.SoGeoCoordinate()
self.triangles = coin.SoIndexedFaceSet()
self.face_material = coin.SoMaterial()
self.edge_material = coin.SoMaterial()
self.edge_style = coin.SoDrawStyle()
self.edge_style.style = coin.SoDrawStyle.LINES
shape_hints = coin.SoShapeHints()
shape_hints.vertex_ordering = coin.SoShapeHints.COUNTERCLOCKWISE
mat_binding = coin.SoMaterialBinding()
mat_binding.value = coin.SoMaterialBinding.PER_FACE
offset = coin.SoPolygonOffset()
offset.styles = coin.SoPolygonOffset.LINES
offset.factor = -2.0
highlight = coin.SoType.fromName("SoFCSelection").createInstance()
highlight.style = "EMISSIVE_DIFFUSE"
highlight.addChild(shape_hints)
highlight.addChild(mat_binding)
highlight.addChild(self.geo_coords)
highlight.addChild(self.triangles)
face = coin.SoSeparator()
face.addChild(self.face_material)
face.addChild(highlight)
edge = coin.SoSeparator()
edge.addChild(self.edge_material)
edge.addChild(self.edge_style)
edge.addChild(highlight)
surface = coin.SoSeparator()
surface.addChild(face)
surface.addChild(offset)
surface.addChild(edge)
wireframe = coin.SoSeparator()
wireframe.addChild(edge)
vobj.addDisplayMode(surface, "Surface")
vobj.addDisplayMode(wireframe, "Wireframe")
self.onChanged(vobj, "ShapeColor")
def updateData(self, obj, prop):
if prop == "VolumeMesh":
mesh = obj.VolumeMesh
if mesh is None or mesh.countPoints() == 0:
return
try:
geo = ["UTM", FreeCAD.ActiveDocument.Site.UtmZone, "FLAT"]
except Exception:
geo = ["UTM", "30N", "FLAT"]
self.geo_coords.geoSystem.setValues(geo)
cm = mesh.copy()
triangles = []
for i in cm.Topology[1]:
triangles.extend(list(i))
triangles.append(-1)
self.geo_coords.point.setValues(cm.Topology[0])
self.triangles.coordIndex.setValues(triangles)
def getIcon(self):
return str(os.path.join(DirIcons, "googleearth.svg"))
def getDisplayModes(self, vobj):
return ["Surface", "Wireframe"]
def getDefaultDisplayMode(self):
return "Surface"
def setDisplayMode(self, mode):
return mode
def __getstate__(self):
return None
def __setstate__(self, state):
return None
# =========================================================================
# TaskPanel
# =========================================================================
class EarthWorksTaskPanel:
def __init__(self):
self.form = FreeCADGui.PySideUic.loadUi(
os.path.join(PVPlantResources.__dir__, "PVPlantEarthworks.ui"))
self.form.setWindowIcon(
QtGui.QIcon(os.path.join(PVPlantResources.DirIcons, "convert.svg")))
def accept(self):
land = FreeCAD.ActiveDocument.Terrain.Mesh.copy()
# Detectar si hay un Platform seleccionado o trackers sueltos
platform_obj = None
frames = []
for obj in FreeCADGui.Selection.getSelection():
if hasattr(obj, "Proxy"):
if obj.Proxy.__class__.__name__ == 'Platform':
platform_obj = obj
t = getattr(obj.Proxy, "Type", None)
if t == "Tracker" and obj not in frames:
frames.append(obj)
elif t == "FrameArea":
for fr in obj.Frames:
if fr not in frames:
frames.append(fr)
if not frames and not platform_obj:
FreeCAD.Console.PrintWarning(
"Selecciona trackers, un FrameArea o un Platform\n")
return False
slope = getattr(FreeCAD.ActiveDocument,
"MaximumWestEastSlope", 10.0)
FreeCAD.ActiveDocument.openTransaction("Movimiento de tierras")
try:
input_data = platform_obj if platform_obj else frames
cut_mesh, fill_mesh, vol_cut, vol_fill = compute_earthworks(
input_data, land, slope)
if cut_mesh and cut_mesh.countPoints() > 3:
v = makeEarthWorksVolume(1) # Cut
v.VolumeMesh = cut_mesh
FreeCAD.Console.PrintMessage(
f"Volumen de corte: {vol_cut:,.0f} mm³\n")
if fill_mesh and fill_mesh.countPoints() > 3:
v = makeEarthWorksVolume(0) # Fill
v.VolumeMesh = fill_mesh
FreeCAD.Console.PrintMessage(
f"Volumen de relleno: {vol_fill:,.0f} mm³\n")
except Exception as e:
FreeCAD.Console.PrintError(
f"Error en movimiento de tierras: {e}\n")
finally:
FreeCAD.ActiveDocument.commitTransaction()
FreeCADGui.Control.closeDialog()
return True
def reject(self):
FreeCADGui.Control.closeDialog()
return True
+403
View File
@@ -0,0 +1,403 @@
# /**********************************************************************
# * *
# * Copyright (c) 2026 Javier Braña <javier.branagutierrez@gmail.com> *
# * *
# * PlacementCalc - Lógica de cálculo de placement de trackers *
# * *
# * Separado de PVPlantPlacement.py para mantener limpio el archivo *
# * de interfaz (TaskPanels, comandos, ViewProviders). *
# * *
# * Funciones exportadas: *
# * - getRows(objs) → listas de filas *
# * - getCols(objs) → listas de columnas *
# * - optimized_cut(L_total, piezas, margen, metodo) *
# * - adjustToTerrain(frames, individual) *
# * - get_trend(points) / getTrend(points) *
# * - getHeadsAndSoil(frame=None) *
# * - moveFrameHead(obj, head, dist) *
# * - selectionFilter(sel, objtype) *
# * - ConvertObjectsTo(sel, objTo) *
# * *
# ***********************************************************************
import FreeCAD
import Part
import math
import numpy as np
# =========================================================================
# selectionFilter
# =========================================================================
def selectionFilter(sel, objtype):
"""Filtra una selección por tipo de Proxy."""
fil = []
for obj in sel:
if hasattr(obj, "Proxy"):
if obj.Proxy.__class__ is objtype:
fil.append(obj)
return fil
# =========================================================================
# optimized_cut
# =========================================================================
def optimized_cut(L_total, piezas, margen=0, metodo='auto'):
"""
Optimiza el corte de piezas en una longitud total.
Similar al algoritmo de corte óptimo de barras.
Args:
L_total: Longitud total disponible
piezas: Lista de longitudes de piezas a cortar
margen: Margen de seguridad por corte
metodo: 'auto', 'greedy' o 'exact'
Returns:
dict con piezas cortadas, desperdicio, etc.
"""
if not piezas:
return {'piezas': [], 'desperdicio': L_total}
piezas_ord = sorted(piezas, reverse=True)
resultado = []
restante = L_total
for pieza in piezas_ord:
if pieza + margen <= restante:
resultado.append(pieza)
restante -= (pieza + margen)
return {
'piezas': resultado,
'desperdicio': restante,
'n_piezas': len(resultado),
'eficiencia': (L_total - restante) / L_total * 100 if L_total > 0 else 0
}
# =========================================================================
# get_trend / getTrend
# =========================================================================
def get_trend(points):
"""
Calcula la tendencia lineal de un conjunto de puntos 3D.
Devuelve (pendiente_x, pendiente_z, intersección) en el plano XZ.
"""
if len(points) < 2:
return 0, 0, 0
xs = np.array([p.x for p in points])
zs = np.array([p.z for p in points])
if np.std(xs) < 1:
return 0, 0, np.mean(zs)
A = np.vstack([xs, np.ones(len(xs))]).T
m, c = np.linalg.lstsq(A, zs, rcond=None)[0]
return m, 0, c
def getTrend(points):
"""Wrapper para compatibilidad (versión antigua)."""
return get_trend(points)
# =========================================================================
# adjustToTerrain
# =========================================================================
def adjustToTerrain(frames, individual=True):
"""
Ajusta la altura de los frames al terreno.
Args:
frames: lista de objetos tracker
individual: si True, ajusta cada frame individualmente.
si False, ajusta por filas.
"""
if not frames:
return
terrain = None
try:
terrain = FreeCAD.ActiveDocument.Site.Terrain
except Exception:
FreeCAD.Console.PrintWarning("No hay terreno en el Site\n")
return
if individual:
for frame in frames:
_adjust_single_frame(frame, terrain)
else:
cols = getCols(list(frames))
if cols:
for col in cols:
for group in col:
if group:
_adjust_frame_group(group, terrain)
def _adjust_single_frame(frame, terrain):
"""Ajusta un frame individual al terreno."""
try:
bb = frame.Shape.BoundBox
center = bb.Center
z_terrain = _get_terrain_z(terrain, center.x, center.y)
if z_terrain is not None:
frame.Placement.Base.z = z_terrain
except Exception:
pass
def _adjust_frame_group(group, terrain):
"""Ajusta un grupo de frames al terreno siguiendo la pendiente."""
if not group:
return
z_values = []
for frame in group:
try:
bb = frame.Shape.BoundBox
center = bb.Center
z = _get_terrain_z(terrain, center.x, center.y)
if z is not None:
z_values.append(z)
except Exception:
z_values.append(None)
valid_zs = [z for z in z_values if z is not None]
if not valid_zs:
return
# Ajustar cada frame a la altura del terreno interpolada
for i, frame in enumerate(group):
if i < len(z_values) and z_values[i] is not None:
try:
frame.Placement.Base.z = z_values[i]
except Exception:
pass
def _get_terrain_z(terrain, x, y):
"""Obtiene la cota Z del terreno en un punto (x, y)."""
try:
if hasattr(terrain, 'Shape') and terrain.Shape:
shape = terrain.Shape
# Proyectar un rayo vertical
p1 = FreeCAD.Vector(x, y, 10000)
p2 = FreeCAD.Vector(x, y, -10000)
dist, pts, info = shape.distToShape(Part.LineSegment(p1, p2).toShape())
if pts:
return pts[0][0].z
except Exception:
pass
return None
# =========================================================================
# getRows / getCols
# =========================================================================
def getRows(objs):
"""
Agrupa objetos tracker en filas según su posición Y y estructura de Placement.
Args:
objs: lista de objetos tracker
Returns:
(rows, columns): tupla de listas de listas
"""
if not objs:
return None, None
# Ordenar por Placement.Base.y
sorted_objs = sorted(objs, key=lambda x: x.Placement.Base.y, reverse=True)
rows = []
processed = set()
for obj in sorted_objs:
if obj.Name in processed:
continue
row = [obj]
processed.add(obj.Name)
base = obj.Placement.Base
for other in sorted_objs:
if other.Name in processed:
continue
# Misma fila si están alineados en Y (misma posición de fila)
if abs(other.Placement.Base.y - base.y) < 5000:
row.append(other)
processed.add(other.Name)
rows.append(row)
# Ordenar cada fila por X
for row in rows:
row.sort(key=lambda x: x.Placement.Base.x)
# Calcular columnas
columns = _compute_columns(rows)
return rows, columns
def getCols(objs):
"""
Agrupa objetos tracker en columnas.
Args:
objs: lista de objetos tracker
Returns:
list: columnas, donde cada columna es una lista de grupos (filas)
"""
rows, columns = getRows(objs)
return columns
def getCols_old(sel, tolerance=4000, sort=True):
"""Versión antigua de getCols, mantenida por compatibilidad."""
if not sel:
return []
# Ordenar por Y descendente
sorted_sel = sorted(sel, key=lambda x: x.Placement.Base.y, reverse=True)
cols = []
used = set()
for obj in sorted_sel:
if obj.Name in used:
continue
fila = [obj]
used.add(obj.Name)
base_x = obj.Placement.Base.x
for other in sorted_sel:
if other.Name in used:
continue
if abs(other.Placement.Base.x - base_x) <= tolerance:
fila.append(other)
used.add(other.Name)
if sort:
fila.sort(key=lambda x: x.Placement.Base.y, reverse=True)
cols.append(fila)
return cols
def _compute_columns(rows):
"""
Calcula la estructura de columnas a partir de las filas.
Cada columna agrupa los frames en la misma posición X vertical.
"""
if not rows:
return []
from collections import defaultdict
# Mapa: posición X → lista de frames
col_map = defaultdict(list)
for row in rows:
for i, frame in enumerate(row):
col_map[i].append(frame)
columns = []
for idx in sorted(col_map.keys()):
col = col_map[idx]
columns.append(col)
return columns
# =========================================================================
# getHeadsAndSoil / moveFrameHead
# =========================================================================
def getHeadsAndSoil(frame=None):
"""
Obtiene las cabezas y suelos de un tracker (o del documento activo).
"""
if frame:
frames = [frame]
else:
try:
frames = [o for o in FreeCAD.ActiveDocument.Objects
if hasattr(o, 'Proxy') and getattr(o.Proxy, 'Type', None) == 'Tracker']
except Exception:
return [], []
heads = []
soils = []
for f in frames:
try:
if hasattr(f, 'HeadPoints'):
heads.extend(f.HeadPoints)
if hasattr(f, 'SoilPoints'):
soils.extend(f.SoilPoints)
except Exception:
pass
return heads, soils
def moveFrameHead(obj, head=0, dist=0):
"""
Mueve la cabeza de un tracker una distancia determinada.
Args:
obj: objeto tracker
head: 0=izquierda, 1=derecha
dist: distancia a mover (mm)
"""
try:
if not hasattr(obj, 'Proxy') or getattr(obj.Proxy, 'Type', None) != 'Tracker':
return
# Lógica de movimiento basada en Placement
placement = obj.Placement
direction = placement.Rotation.multVec(FreeCAD.Vector(1, 0, 0))
if head == 0:
placement.Base = placement.Base - direction * dist
else:
placement.Base = placement.Base + direction * dist
obj.Placement = placement
except Exception:
pass
# =========================================================================
# ConvertObjectsTo
# =========================================================================
def ConvertObjectsTo(sel, objTo):
"""
Convierte objetos seleccionados a otro tipo.
Args:
sel: lista de objetos seleccionados
objTo: clase destino (FeaturePython)
"""
if not sel or not objTo:
return
for obj in sel:
try:
if hasattr(obj, "Proxy"):
isFrame = obj.Proxy.__class__ is objTo
# Si ya es del tipo destino, se salta
if isFrame:
continue
# Crear nuevo objeto del tipo destino
if hasattr(obj, "Shape") and obj.Shape:
new_obj = FreeCAD.ActiveDocument.addObject(
"Part::FeaturePython", obj.Name + "_converted")
# Aquí iría la lógica específica de conversión
# dependiendo del tipo de objeto origen y destino
FreeCAD.Console.PrintMessage(
f"Convertido {obj.Label}\n")
except Exception:
FreeCAD.Console.PrintWarning(
f"No se pudo convertir {obj.Label}\n")
+468
View File
@@ -0,0 +1,468 @@
# /**********************************************************************
# * *
# * Copyright (c) 2026 Javier Braña <javier.branagutierrez@gmail.com> *
# * *
# * PVPlantPlatform - Plataforma de diseño solar *
# * *
# * Es el elemento central del movimiento de tierras. Representa la *
# * superficie diseñada generada a partir de la disposición de trackers.*
# * *
# * De ella dependen: *
# * - EarthWorks: cut/fill entre plataforma y terreno natural *
# * - Road: trazado de viales sobre la plataforma *
# * - Drainage: drenaje superficial *
# * - Trench: zanjas sobre la plataforma *
# * *
# ***********************************************************************
import FreeCAD
import Part
import math
if FreeCAD.GuiUp:
import FreeCADGui, os
from PySide.QtCore import QT_TRANSLATE_NOOP
else:
def QT_TRANSLATE_NOOP(ctxt, txt): return txt
import PVPlantResources
from PVPlantResources import DirIcons as DirIcons
# =========================================================================
# Constructor
# =========================================================================
def make_platform(frames=None, name="Platform"):
"""
Crea un objeto Platform en el documento activo.
Args:
frames: lista opcional de objetos tracker para inicializar
name: nombre del objeto
Returns:
Objeto FeaturePython Platform, o None si no hay documento
"""
doc = FreeCAD.ActiveDocument
if doc is None:
return None
obj = doc.addObject("Part::FeaturePython", name)
Platform(obj)
_ViewProviderPlatform(obj.ViewObject)
obj.Label = name
if frames:
# Asignar SourceFrames como lista de enlaces
obj.SourceFrames = frames
doc.recompute()
return obj
# =========================================================================
# FeaturePython: Platform
# =========================================================================
class Platform:
"""
Plataforma de diseño generada desde trackers solares.
Propiedades principales:
SourceFrames : Lista de trackers que definen la plataforma
SlopeTolerance : Pendiente máxima E-W (grados)
PlatformArea : Área de la plataforma (solo lectura)
PlatformVolume : Volumen bajo la plataforma (solo lectura)
Status : Estado del último cálculo
"""
def __init__(self, obj):
self.setProperties(obj)
obj.Proxy = self
def setProperties(self, obj):
pl = obj.PropertiesList
if "SourceFrames" not in pl:
obj.addProperty(
"App::PropertyLinkList", "SourceFrames",
"Platform",
"Trackers que definen la plataforma")
if "SlopeTolerance" not in pl:
obj.addProperty(
"App::PropertyFloat", "SlopeTolerance",
"Platform",
"Pendiente transversal máxima (grados)").SlopeTolerance = 10.0
if "PlatformArea" not in pl:
obj.addProperty(
"App::PropertyArea", "PlatformArea",
"Platform",
"Área total de la plataforma (solo lectura)")
obj.setEditorMode("PlatformArea", 1)
if "PlatformVolume" not in pl:
obj.addProperty(
"App::PropertyVolume", "PlatformVolume",
"Platform",
"Volumen bajo la plataforma (solo lectura)")
obj.setEditorMode("PlatformVolume", 1)
if "NumberOfFrames" not in pl:
obj.addProperty(
"App::PropertyInteger", "NumberOfFrames",
"Platform",
"Número de trackers en la plataforma")
obj.setEditorMode("NumberOfFrames", 1)
if "Status" not in pl:
obj.addProperty(
"App::PropertyString", "Status",
"Platform",
"Estado del último cálculo")
def onDocumentRestored(self, obj):
self.setProperties(obj)
def execute(self, obj):
"""Calcula la plataforma a partir de los SourceFrames."""
frames = obj.SourceFrames
if not frames:
obj.Shape = Part.Shape()
obj.Status = "Sin frames"
return
obj.NumberOfFrames = len(frames)
slope = obj.SlopeTolerance
try:
shape = _build_platform_shape(frames, slope)
except Exception as e:
obj.Status = f"Error: {e}"
FreeCAD.Console.PrintError(
f"Error al generar plataforma: {e}\n")
return
if shape is None:
obj.Status = "No se pudo generar"
return
obj.Shape = shape
# Calcular área y volumen
try:
area = shape.Area
if area > 0:
obj.PlatformArea = area
# Volumen aproximado: proyectar al plano XY
try:
obj.PlatformVolume = shape.Volume
except Exception:
pass
except Exception:
pass
obj.Status = f"OK - {len(frames)} frames"
FreeCAD.Console.PrintMessage(
f"Plataforma generada: {len(frames)} frames, "
f"área={area:,.0f} mm²\n")
def __getstate__(self):
return None
def __setstate__(self, state):
return None
# =========================================================================
# Cálculo de la plataforma (lógica principal)
# =========================================================================
def _build_platform_shape(frames, slope_tolerance):
"""
Construye la geometría de la plataforma desde los frames.
Returns:
Part.Solid o None si falla
"""
rows, columns = _get_tracker_rows(frames)
if rows is None or not rows:
return None
all_faces = []
tools = []
# Fase 1: Lofts longitudinales (a lo largo de cada fila)
for group in rows:
lines = _generate_row_lines(group, slope_tolerance)
tools.extend(lines["tools"])
if len(lines["edges"]) >= 2:
try:
loft = Part.makeLoft(lines["edges"], False, True, False)
if loft and not loft.isNull():
all_faces.extend(loft.Faces)
except Exception:
pass
# Fase 2: Lofts transversales (entre columnas)
if columns:
for group in rows:
for frame in group:
col, idx = _find_in_columns(frame, columns)
tool = _find_tool(frame, tools)
if tool is None or idx >= len(col) - 1:
continue
next_frame = col[idx + 1]
next_tool = _find_tool(next_frame, tools)
if next_tool is None:
continue
try:
l1 = Part.LineSegment(
tool[1].Vertexes[-1].Point,
next_tool[1].Vertexes[0].Point
).toShape()
l2 = Part.LineSegment(
tool[2].Vertexes[-1].Point,
next_tool[2].Vertexes[0].Point
).toShape()
if l1 and l2:
loft = Part.makeLoft([l1, l2], False, True, False)
if loft and not loft.isNull():
all_faces.extend(loft.Faces)
except Exception:
pass
if not all_faces:
return None
# Fase 3: Unir caras en un sólido
try:
platform = None
for face in all_faces:
if platform is None:
platform = face
else:
try:
platform = platform.fuse(face)
except Exception:
pass
if platform is None:
return None
if platform.ShapeType == "Shell":
try:
platform = Part.makeSolid(platform)
except Exception:
pass
elif platform.ShapeType == "Compound":
faces_in = [s for s in platform.SubShapes if s.ShapeType == "Face"]
if faces_in:
try:
shell = Part.makeShell(faces_in)
platform = Part.makeSolid(shell)
except Exception:
pass
return platform if not platform.isNull() else None
except Exception:
return None
def _get_tracker_rows(frames):
"""Agrupa trackers usando la lógica de PVPlantPlacement."""
try:
import PVPlantPlacement
return PVPlantPlacement.getRows(frames)
except Exception:
return None, None
def _generate_row_lines(group, slope_tolerance):
"""
Genera líneas de borde (izquierda/derecha) para una fila de trackers.
Returns:
dict con edges (lista de Part.Shape) y tools (lista de [frame, izq, der])
"""
result = {"edges": [], "tools": []}
for i, frame in enumerate(group):
if not hasattr(frame, "Setup"):
continue
aw = _angle_to_prev(group, i)
ae = _angle_to_next(group, i)
anf = (aw + ae) / 2
if anf > slope_tolerance:
anf = slope_tolerance
wdt = _get_half_width(frame)
zz = wdt * math.sin(math.radians(anf))
base = _get_base_line(frame)
li = base.copy()
li.Placement = frame.Placement
li.Placement.Rotation = frame.Placement.Rotation
li.Placement.Base.x -= wdt
li.Placement.Base.z -= zz
result["edges"].append(li)
ld = base.copy()
ld.Placement = frame.Placement
ld.Placement.Rotation = frame.Placement.Rotation
ld.Placement.Base.x += wdt
ld.Placement.Base.z += zz
result["edges"].append(ld)
result["tools"].append([frame, li, ld])
return result
def _get_half_width(frame):
try:
return int(frame.Setup.Width / 2)
except Exception:
return 0
def _get_base_line(frame):
try:
lng = int(frame.Setup.Length / 2)
return Part.LineSegment(
FreeCAD.Vector(-lng, 0, 0),
FreeCAD.Vector(lng, 0, 0)
).toShape()
except Exception:
try:
bb = frame.Setup.Shape.BoundBox
return Part.LineSegment(
FreeCAD.Vector(bb.XMin, 0, 0),
FreeCAD.Vector(bb.XMax, 0, 0)
).toShape()
except Exception:
return Part.LineSegment(
FreeCAD.Vector(-2000, 0, 0),
FreeCAD.Vector(2000, 0, 0)
).toShape()
def _angle_to_prev(group, i):
if i <= 0:
return 0
return _angle_xz(
group[i - 1].Placement.Base,
group[i].Placement.Base
)
def _angle_to_next(group, i):
if i >= len(group) - 1:
return 0
return _angle_xz(
group[i].Placement.Base,
group[i + 1].Placement.Base
)
def _angle_xz(v1, v2):
dx = v2.x - v1.x
dz = v2.z - v1.z
return math.degrees(math.atan2(dz, dx))
def _find_in_columns(frame, columns):
for col in columns:
for g in col:
if frame in g:
return g, g.index(frame)
return [], -1
def _find_tool(frame, tools):
for t in tools:
if t[0] == frame:
return t
return None
# =========================================================================
# ViewProvider
# =========================================================================
class _ViewProviderPlatform:
def __init__(self, vobj):
vobj.Proxy = self
pl = vobj.PropertiesList
if "Transparency" not in pl:
vobj.addProperty(
"App::PropertyIntegerConstraint", "Transparency",
"Platform Style", "Transparencia de la plataforma")
vobj.Transparency = (40, 0, 100, 1)
if "ShapeColor" not in pl:
vobj.addProperty(
"App::PropertyColor", "ShapeColor",
"Platform Style", "Color de la plataforma")
vobj.ShapeColor = (0.3, 0.8, 0.3, 0.6) # verde semitransparente
if "ShapeMaterial" not in pl:
vobj.addProperty(
"App::PropertyMaterial", "ShapeMaterial",
"Platform Style", "Material")
vobj.ShapeMaterial = FreeCAD.Material()
vobj.ShapeMaterial.DiffuseColor = vobj.ShapeColor
def onChanged(self, vobj, prop):
if prop in ("ShapeColor", "Transparency"):
if hasattr(vobj, "ShapeColor") and hasattr(vobj, "Transparency"):
c = vobj.ShapeColor
t = vobj.Transparency
vobj.ShapeMaterial.DiffuseColor = (c[0], c[1], c[2], t / 100)
def getIcon(self):
return str(os.path.join(DirIcons, "solar-fixed.svg"))
def __getstate__(self):
return None
def __setstate__(self, state):
return None
# =========================================================================
# Functions de conveniencia (API pública)
# =========================================================================
def build_platform(frames, slope_tolerance=10.0):
"""
API pública: construye la geometría de plataforma desde frames.
Útil para EarthWorks, Road, etc. que quieran la Shape sin crear objeto.
Returns:
Part.Solid o None
"""
return _build_platform_shape(frames, slope_tolerance)
def get_platform_shape(platform_obj):
"""
Obtiene la Shape de un objeto Platform de forma segura.
"""
if platform_obj is None:
return None
try:
shape = platform_obj.Shape
if shape and not shape.isNull():
return shape
except Exception:
pass
return None
+1 -1
View File
@@ -114,7 +114,7 @@ def makeTrench(base=None):
try:
folder = FreeCAD.ActiveDocument.Trenches
except:
except AttributeError:
folder = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", 'Trenches')
folder.Label = "Trenches"
folder.addObject(obj)
+1 -1
View File
@@ -5,7 +5,7 @@ import zipfile
import tempfile
import shutil
import xml.etree.ElementTree as ET
from PySide2 import QtWidgets, QtCore, QtGui
from PySide import QtWidgets, QtCore, QtGui
import FreeCAD
import Mesh
import Part
+2 -2
View File
@@ -72,11 +72,11 @@ class _PVPlantImportDXF:
def openFile(self):
''' '''
"getOpenFileName(parent: typing.Union[PySide2.QtWidgets.QWidget, NoneType] = None," \
"getOpenFileName(parent: typing.Union[PySide.QtWidgets.QWidget, NoneType] = None," \
"caption: str = ''," \
"dir: str = ''," \
"filter: str = ''," \
"options: PySide2.QtWidgets.QFileDialog.Options = Default(QFileDialog.Options)) -> typing.Tuple[str, str]"
"options: PySide.QtWidgets.QFileDialog.Options = Default(QFileDialog.Options)) -> typing.Tuple[str, str]"
filename, trash = QtGui.QFileDialog().getOpenFileName(None, 'Select File', os.getcwd(), 'Autocad dxf (*.dxf)')
if filename == "":
return
+1
View File
@@ -7,6 +7,7 @@ import ssl
import certifi
import urllib.request
import math
import utm
from collections import defaultdict
import PVPlantImportGrid as ImportElevation
-554
View File
@@ -1,554 +0,0 @@
import ArchComponent
import FreeCAD
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtCore, QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
else:
# \cond
def translate(ctxt, txt):
return txt
def QT_TRANSLATE_NOOP(ctxt, txt):
return txt
# \endcond
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
import threading
def makePlacement(baseobj=None, diameter=0, length=0, placement=None, name="Placement"):
"makePipe([baseobj,diamerter,length,placement,name]): creates an pipe object from the given base object"
if not FreeCAD.ActiveDocument:
FreeCAD.Console.PrintError("No active document. Aborting\n")
return
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", name)
obj.Label = name
_PVPlantPlacement(obj)
if FreeCAD.GuiUp:
_ViewProviderPVPlantPlacement(obj.ViewObject)
if baseobj:
baseobj.ViewObject.hide()
return obj
class _CommandPVPlantPlacement:
"the Arch Schedule command definition"
def GetResources(self):
return {'Pixmap': 'Placement',
'Accel': "P, S",
'MenuText': QT_TRANSLATE_NOOP("Placement", "Placement"),
'ToolTip': QT_TRANSLATE_NOOP("Placement", "Crear un campo fotovoltaico")}
def Activated(self):
taskd = _PVPlantPlacementTaskPanel()
FreeCADGui.Control.showDialog(taskd)
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
class _PVPlantPlacement(ArchComponent.Component):
"the PVPlantPlacement object"
def __init__(self, obj):
ArchComponent.Component.__init__(self, obj)
self.setProperties(obj)
# Does a IfcType exist?
# obj.IfcType = "Fence"
obj.MoveWithHost = False
def setProperties(self, obj):
ArchComponent.Component.setProperties(self, obj)
pl = obj.PropertiesList
if not "Section" in pl:
obj.addProperty("App::PropertyLink", "Land", "Placement", QT_TRANSLATE_NOOP(
"App::Property", "A single section of the fence"))
if not "Post" in pl:
obj.addProperty("App::PropertyLink", "Structure", "Placement", QT_TRANSLATE_NOOP(
"App::Property", "A single fence post"))
if not "Path" in pl:
obj.addProperty("App::PropertyLink", "Path", "Placement", QT_TRANSLATE_NOOP(
"App::Property", "The Path the fence should follow"))
if not "NumberOfSections" in pl:
obj.addProperty("App::PropertyInteger", "NumberOfSections", "Count", QT_TRANSLATE_NOOP(
"App::Property", "The number of sections the fence is built of"))
obj.setEditorMode("NumberOfSections", 1)
if not "NumberOfPosts" in pl:
obj.addProperty("App::PropertyInteger", "NumberOfPosts", "Count", QT_TRANSLATE_NOOP(
"App::Property", "The number of posts used to build the fence"))
obj.setEditorMode("NumberOfPosts", 1)
self.Type = "Fence"
def execute(self, obj):
# fills columns A, B and C of the spreadsheet
if not obj.Description:
return
def __getstate__(self):
return self.Type
def __setstate__(self, state):
if state:
self.Type = state
class _ViewProviderPVPlantPlacement:
"A View Provider for PVPlantPlacement"
def __init__(self, vobj):
vobj.Proxy = self
def getIcon(self):
return ":/icons/Arch_Schedule.svg"
def attach(self, vobj):
self.Object = vobj.Object
def setEdit(self, vobj, mode):
# taskd = _ArchScheduleTaskPanel(vobj.Object)
# FreeCADGui.Control.showDialog(taskd)
return True
def doubleClicked(self, vobj):
# taskd = _ArchScheduleTaskPanel(vobj.Object)
# FreeCADGui.Control.showDialog(taskd)
return True
def unsetEdit(self, vobj, mode):
# FreeCADGui.Control.closeDialog()
return
def claimChildren(self):
# if hasattr(self,"Object"):
# return [self.Object.Result]
return None
def __getstate__(self):
return None
def __setstate__(self, state):
return None
def getDisplayModes(self, vobj):
return ["Default"]
def getDefaultDisplayMode(self):
return "Default"
def setDisplayMode(self, mode):
return mode
class _PVPlantPlacementTaskPanel:
'''The editmode TaskPanel for Schedules'''
def __init__(self, obj=None):
self.Terrain = None
self.Rack = None
self.Gap = 200
self.Pitch = 4500
# form:
self.form = QtGui.QWidget()
self.form.resize(800, 640)
self.form.setWindowTitle("Curvas de nivel")
self.form.setWindowIcon(QtGui.QIcon(":/icons/Arch_Schedule.svg"))
self.grid = QtGui.QGridLayout(self.form)
# parameters
self.labelTerrain = QtGui.QLabel()
self.labelTerrain.setText("Terreno:")
self.lineTerrain = QtGui.QLineEdit(self.form)
self.lineTerrain.setObjectName(_fromUtf8("lineTerrain"))
self.lineTerrain.readOnly = True
self.grid.addWidget(self.labelTerrain, self.grid.rowCount(), 0, 1, 1)
self.grid.addWidget(self.lineTerrain, self.grid.rowCount() - 1, 1, 1, 1)
self.buttonAddTerrain = QtGui.QPushButton('Sel')
self.grid.addWidget(self.buttonAddTerrain, self.grid.rowCount() - 1, 2, 1, 1)
self.labelRack = QtGui.QLabel()
self.labelRack.setText("Rack:")
self.lineRack = QtGui.QLineEdit(self.form)
self.lineRack.setObjectName(_fromUtf8("lineRack"))
self.lineRack.readOnly = True
self.grid.addWidget(self.labelRack, self.grid.rowCount(), 0, 1, 1)
self.grid.addWidget(self.lineRack, self.grid.rowCount() - 1, 1, 1, 1)
self.buttonAddRack = QtGui.QPushButton('Sel')
self.grid.addWidget(self.buttonAddRack, self.grid.rowCount() - 1, 2, 1, 1)
self.line1 = QtGui.QFrame()
self.line1.setFrameShape(QtGui.QFrame.HLine)
self.line1.setFrameShadow(QtGui.QFrame.Sunken)
self.grid.addWidget(self.line1, self.grid.rowCount(), 0, 1, -1)
self.labelTypeStructure = QtGui.QLabel()
self.labelTypeStructure.setText("Tipo de estructura:")
self.valueTypeStructure = QtGui.QComboBox()
self.valueTypeStructure.addItems(["Fixed", "Tracker 1 Axis"])
self.valueTypeStructure.setCurrentIndex(0)
self.grid.addWidget(self.labelTypeStructure, self.grid.rowCount(), 0, 1, 1)
self.grid.addWidget(self.valueTypeStructure, self.grid.rowCount() - 1, 1, 1, -1)
self.labelOrientation = QtGui.QLabel()
self.labelOrientation.setText("Orientacion:")
self.valueOrientation = QtGui.QComboBox()
self.valueOrientation.addItems(["Norte-Sur", "Este-Oeste"])
self.valueOrientation.setCurrentIndex(0)
self.grid.addWidget(self.labelOrientation, self.grid.rowCount(), 0, 1, 1)
self.grid.addWidget(self.valueOrientation, self.grid.rowCount() - 1, 1, 1, -1)
self.labelGap = QtGui.QLabel()
self.labelGap.setText("Espacio entre Columnas:")
self.valueGap = FreeCADGui.UiLoader().createWidget("Gui::InputField")
self.valueGap.setText(str(self.Gap) + " mm")
self.grid.addWidget(self.labelGap, self.grid.rowCount(), 0, 1, 1)
self.grid.addWidget(self.valueGap, self.grid.rowCount() - 1, 1, 1, -1)
self.labelPitch = QtGui.QLabel()
self.labelPitch.setText("Separacion entre Filas:")
self.valuePitch = FreeCADGui.UiLoader().createWidget("Gui::InputField")
self.valuePitch.setText(str(self.Pitch) + " mm")
self.grid.addWidget(self.labelPitch, self.grid.rowCount(), 0, 1, 1)
self.grid.addWidget(self.valuePitch, self.grid.rowCount() - 1, 1, 1, -1)
self.labelAlign = QtGui.QLabel()
self.labelAlign.setText("Método de alineación:")
self.valueAlign = QtGui.QComboBox()
self.valueAlign.addItems(["Si", "No"])
self.valueAlign.setCurrentIndex(0)
self.grid.addWidget(self.labelAlign, self.grid.rowCount(), 0, 1, 1)
self.grid.addWidget(self.valueAlign, self.grid.rowCount() - 1, 1, 1, -1)
self.line2 = QtGui.QFrame()
self.line2.setFrameShape(QtGui.QFrame.HLine)
self.line2.setFrameShadow(QtGui.QFrame.Sunken)
self.grid.addWidget(self.line2, self.grid.rowCount(), 0, 1, -1)
self.labelSideSlope = QtGui.QLabel()
self.labelSideSlope.setText("Maxima inclinacion longitudinal:")
self.valueSideSlope = FreeCADGui.UiLoader().createWidget("Gui::InputField")
self.valueSideSlope.setText("15")
self.grid.addWidget(self.labelSideSlope, self.grid.rowCount(), 0, 1, 1)
self.grid.addWidget(self.valueSideSlope, self.grid.rowCount() - 1, 1, 1, -1)
QtCore.QObject.connect(self.buttonAddTerrain, QtCore.SIGNAL("clicked()"), self.addTerrain)
QtCore.QObject.connect(self.buttonAddRack, QtCore.SIGNAL("clicked()"), self.addRack)
# QtCore.QObject.connect(self.form.buttonDel, QtCore.SIGNAL("clicked()"), self.remove)
# QtCore.QObject.connect(self.form.buttonClear, QtCore.SIGNAL("clicked()"), self.clear)
# QtCore.QObject.connect(self.form.buttonSelect, QtCore.SIGNAL("clicked()"), self.select)
def addTerrain(self):
sel = FreeCADGui.Selection.getSelection()
if len(sel) > 0:
self.Terrain = sel[0]
self.lineTerrain.setText(self.Terrain.Label)
def addRack(self):
sel = FreeCADGui.Selection.getSelection()
if len(sel) > 0:
self.Rack = sel[0]
self.lineRack.setText(self.Rack.Label)
def accept(self):
if self.Terrain is not None and self.Rack is not None:
self.Gap = FreeCAD.Units.Quantity(self.valueGap.text()).Value
self.Pitch = FreeCAD.Units.Quantity(self.valuePitch.text()).Value
self.placement()
return True
def placement(self):
if self.valueTypeStructure.currentIndex() == 0: # Fixed
print("Rack")
else:
print("Tracker")
if self.Rack.Height < self.Rack.Length:
print("rotar")
aux = self.Rack.Length
self.Rack.Length = self.Rack.Height
self.Rack.Height = aux
self.Rack.Placement.Base.x = self.Terrain.Shape.BoundBox.XMin
self.Rack.Placement.Base.y = self.Terrain.Shape.BoundBox.YMin
DistColls = self.Rack.Length.Value + self.Gap
DistRows = self.Rack.Height.Value + self.Pitch
area = self.Rack.Shape.Faces[0].Area # * 0.999999999
import Draft
rec = Draft.makeRectangle(length=self.Terrain.Shape.BoundBox.XLength, height=self.Rack.Height, face=True,
support=None)
rec.Placement.Base.x = self.Terrain.Shape.BoundBox.XMin
rec.Placement.Base.y = self.Terrain.Shape.BoundBox.YMin
try:
while rec.Shape.BoundBox.YMax <= self.Terrain.Shape.BoundBox.YMax:
common = self.Terrain.Shape.common(rec.Shape)
for shape in common.Faces:
if shape.Area >= area:
if False:
minorPoint = FreeCAD.Vector(0, 0, 0)
for spoint in shape.OuterWire.Vertexes:
if minorPoint.y >= spoint.Point.y:
if minorPoint.x >= spoint.x:
minorPoint = spoint
self.Rack.Placement.Base = spoint
else:
# más rápido
self.Rack.Placement.Base.x = shape.BoundBox.XMin
self.Rack.Placement.Base.y = shape.BoundBox.YMin
while self.Rack.Shape.BoundBox.XMax <= shape.BoundBox.XMax:
verts = [v.Point for v in rackClone.Shape.OuterWire.OrderedVertexes]
inside = True
for vert in verts:
if not shape.isInside(vert, 0, True):
inside = False
break
if inside:
raise
else:
# ajuste fino hasta encontrar el primer sitio:
rackClone.Placement.Base.x += 100 # un metro
'''old version
common1 = shape.common(self.Rack.Shape)
if common1.Area >= area:
raise
else:
# ajuste fino hasta encontrar el primer sitio:
self.Rack.Placement.Base.x += 500 # un metro
del common1
'''
# ajuste fino hasta encontrar el primer sitio:
rec.Placement.Base.y += 100
del common
except:
pass
#print("Found")
FreeCAD.ActiveDocument.removeObject(rec.Name)
from datetime import datetime
starttime = datetime.now()
if self.valueOrientation.currentIndex() == 0:
# Código para crear filas:
self.Rack.Placement.Base.x = self.Terrain.Shape.BoundBox.XMin
i = 1
yy = self.Rack.Placement.Base.y
while yy < self.Terrain.Shape.BoundBox.YMax:
CreateRow1(self.Rack.Placement.Base.x, yy, self.Rack, self.Terrain, DistColls, area, i)
i += 1
yy += DistRows
elif self.valueOrientation.currentIndex() == 2:
# Código para crear columnas:
while self.Rack.Placement.Base.x > self.Terrain.Shape.BoundBox.XMin:
self.Rack.Placement.Base.x -= DistColls
else:
xx = self.Rack.Placement.Base.x
while xx < self.Terrain.Shape.BoundBox.XMax:
CreateGrid(xx, self.Rack.Placement.Base.y, self.Rack, self.Terrain, DistRows, area)
xx += DistColls
FreeCAD.activeDocument().recompute()
print("Everything OK (", datetime.now() - starttime, ")")
# Alinear solo filas. las columnas donde se pueda
def CreateRow(XX, YY, rack, land, gap, area, rowNumber):
import Draft
rackClone = Draft.makeRectangle(length=rack.Length, height=rack.Height, face=True, support=None)
rackClone.Label = 'rackClone{a}'.format(a=rowNumber)
rackClone.Placement.Base.x = XX
rackClone.Placement.Base.y = YY
rec = Draft.makeRectangle(length=land.Shape.BoundBox.XLength, height=rack.Height, face=True, support=None)
rec.Placement.Base.x = land.Shape.BoundBox.XMin
rec.Placement.Base.y = YY
FreeCAD.activeDocument().recompute()
common = land.Shape.common(rec.Shape)
for shape in common.Faces:
if shape.Area >= area:
rackClone.Placement.Base.x = shape.BoundBox.XMin
rackClone.Placement.Base.y = shape.BoundBox.YMin
while rackClone.Shape.BoundBox.XMax <= shape.BoundBox.XMax:
common1 = shape.common(rackClone.Shape)
if common1.Area >= area:
tmp = Draft.makeRectangle(length=rack.Length, height=rack.Height, placement=rackClone.Placement,
face=True, support=None)
tmp.Label = 'R{:03}-000'.format(rowNumber)
rackClone.Placement.Base.x += gap
else:
# ajuste fino hasta encontrar el primer sitio:
rackClone.Placement.Base.x += 500 # un metro
del common1
del common
FreeCAD.ActiveDocument.removeObject(rackClone.Name)
FreeCAD.ActiveDocument.removeObject(rec.Name)
# Alinear solo filas. las columnas donde se pueda
def CreateRow1(XX, YY, rack, land, gap, area, rowNumber):
import Draft
rackClone = Draft.makeRectangle(length=rack.Length, height=rack.Height, face=True, support=None)
rackClone.Label = 'rackClone{a}'.format(a=rowNumber)
rackClone.Placement.Base.x = XX
rackClone.Placement.Base.y = YY
rec = Draft.makeRectangle(length=land.Shape.BoundBox.XLength, height=rack.Height, face=True, support=None)
rec.Placement.Base.x = land.Shape.BoundBox.XMin
rec.Placement.Base.y = YY
FreeCAD.activeDocument().recompute()
common = land.Shape.common(rec.Shape)
for shape in common.Faces:
if shape.Area >= area:
if False:
minorPoint = FreeCAD.Vector(0, 0, 0)
for spoint in shape.OuterWire.Vertexes:
if minorPoint.y >= spoint.Point.y:
if minorPoint.x >= spoint.x:
minorPoint = spoint
rackClone.Placement.Base = spoint
else:
# más rápido
rackClone.Placement.Base.x = shape.BoundBox.XMin
rackClone.Placement.Base.y = shape.BoundBox.YMin
while rackClone.Shape.BoundBox.XMax <= shape.BoundBox.XMax:
verts = [v.Point for v in rackClone.Shape.OuterWire.OrderedVertexes]
inside = True
for vert in verts:
if not shape.isInside(vert, 0, True):
inside = False
break
if inside:
#tmp = rack.Shape.copy()
#tmp.Placement = rack.Placement
tmp = Draft.makeRectangle(length=rack.Length, height=rack.Height, placement=rackClone.Placement,
face=True, support=None)
tmp.Label = 'R{:03}-000'.format(rowNumber)
rackClone.Placement.Base.x += gap
else:
# ajuste fino hasta encontrar el primer sitio:
rackClone.Placement.Base.x += 500 # un metro
del common
FreeCAD.ActiveDocument.removeObject(rackClone.Name)
FreeCAD.ActiveDocument.removeObject(rec.Name)
# Alinear columna y fila (grid perfecta)
def CreateGrid(XX, YY, rack, land, gap, area):
print("CreateGrid")
import Draft
rackClone = Draft.makeRectangle(length=rack.Length, height=rack.Height, face=True, support=None)
rackClone.Label = 'rackClone{a}'.format(a=XX)
rackClone.Placement.Base.x = XX
rackClone.Placement.Base.y = YY
# if False:
while rackClone.Shape.BoundBox.YMax < land.Shape.BoundBox.YMax:
common = land.Shape.common(rackClone.Shape)
if common.Area >= area:
tmp = Draft.makeRectangle(length=rack.Length, height=rack.Height,
placement=rackClone.Placement, face=True, support=None)
tmp.Label = 'rackClone{a}'.format(a=XX)
rackClone.Placement.Base.y += gap
# else:
# # ajuste fino hasta encontrar el primer sitio:
# rackClone.Placement.Base.y += 1000
FreeCAD.ActiveDocument.removeObject(rackClone.Name)
# Alinear solo filas. las columnas donde se pueda
def CreateCol(XX, YY, rack, land, gap, area):
import Draft
rackClone = Draft.makeRectangle(length=rack.Length, height=rack.Height, face=True, support=None)
rackClone.Label = 'rackClone{a}'.format(a=XX)
rackClone.Placement.Base.x = XX
rackClone.Placement.Base.y = YY
while rackClone.Shape.BoundBox.YMax < land.Shape.BoundBox.YMax:
common = land.Shape.common(rackClone.Shape)
if common.Area >= area:
tmp = Draft.makeRectangle(length=rack.Length, height=rack.Height,
placement=rackClone.Placement, face=True, support=None)
tmp.Label = 'rackClone{a}'.format(a=XX)
rackClone.Placement.Base.y += gap
else:
# ajuste fino hasta encontrar el primer sitio:
rackClone.Placement.Base.y += 100
FreeCAD.ActiveDocument.removeObject(rackClone.Name)
# TODO: Probar a usar hilos:
class _CreateCol(threading.Thread):
def __init__(self, args=()):
super().__init__()
self.XX = args[0]
self.YY = args[1]
self.rack = args[2]
self.land = args[3]
self.gap = args[4]
self.area = args[5]
def run(self):
import Draft
# rackClone = Draft.makeRectangle(length=land.Shape.BoundBox.XLength, height=rack.Height,
# face=True, support=None)
# rackClone = FreeCAD.activeDocument().addObject('Part::Feature')
# rackClone.Shape = self.rack.Shape
rackClone = Draft.makeRectangle(length=self.rack.Length, height=self.rack.Height, face=True, support=None)
rackClone.Label = 'rackClone{a}'.format(a=self.XX)
rackClone.Placement.Base.x = self.XX
rackClone.Placement.Base.y = self.YY
# if False:
while rackClone.Shape.BoundBox.YMax < self.land.Shape.BoundBox.YMax:
common = self.land.Shape.common(rackClone.Shape)
if common.Area >= self.area:
rack = Draft.makeRectangle(length=self.rack.Length, height=self.rack.Height,
placement=rackClone.Placement, face=True, support=None)
rack.Label = 'rackClone{a}'.format(a=self.XX)
rackClone.Placement.Base.y += self.gap
# else:
# # ajuste fino hasta encontrar el primer sitio:
# rackClone.Placement.Base.y += 1000
# FreeCAD.ActiveDocument.removeObject(rackClone.Name)
if FreeCAD.GuiUp:
FreeCADGui.addCommand('PVPlantPlacement', _CommandPVPlantPlacement())
-5
View File
@@ -1,5 +0,0 @@
# PVPlant - Paquete reestructurado
#
# Los imports legacy (from PVPlantSite import X, etc.) siguen funcionando.
# Para nuevo código, usar: from PVPlant.core.site import _PVPlantSite
View File
-422
View File
@@ -1,422 +0,0 @@
# /**********************************************************************
# * *
# * 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
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtCore, QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
import os
else:
# \cond
def translate(ctxt,txt):
return txt
def QT_TRANSLATE_NOOP(ctxt,txt):
return txt
# \endcond
import PVPlantResources
from PVPlantResources import DirIcons as DirIcons
from PVPlantResources import DirResources as DirResources
class MapWindow(QtGui.QWidget):
def __init__(self, WinTitle="MapWindow"):
super(MapWindow, self).__init__()
self.raise_()
self.lat = None
self.lon = None
self.minLat = None
self.maxLat = None
self.minLon = None
self.maxLon = None
self.zoom = None
self.WinTitle = WinTitle
self.georeference_coordinates = {'lat': None, 'lon': None}
self.setupUi()
def setupUi(self):
from PySide2.QtWebEngineWidgets import QWebEngineView
from PySide2.QtWebChannel import QWebChannel
self.ui = FreeCADGui.PySideUic.loadUi(PVPlantResources.__dir__ + "/PVPlantGeoreferencing.ui", self)
self.resize(1200, 800)
self.setWindowTitle(self.WinTitle)
self.setWindowIcon(QtGui.QIcon(os.path.join(DirIcons, "Location.svg")))
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.layout = QtGui.QHBoxLayout(self)
self.layout.setContentsMargins(4, 4, 4, 4)
LeftWidget = QtGui.QWidget(self)
LeftLayout = QtGui.QVBoxLayout(LeftWidget)
LeftWidget.setLayout(LeftLayout)
LeftLayout.setContentsMargins(0, 0, 0, 0)
RightWidget = QtGui.QWidget(self)
RightWidget.setFixedWidth(350)
RightLayout = QtGui.QVBoxLayout(RightWidget)
RightWidget.setLayout(RightLayout)
RightLayout.setContentsMargins(0, 0, 0, 0)
self.layout.addWidget(LeftWidget)
self.layout.addWidget(RightWidget)
# Left Widgets:
# -- Search Bar:
self.valueSearch = QtGui.QLineEdit(self)
self.valueSearch.setPlaceholderText("Search")
self.valueSearch.returnPressed.connect(self.onSearch)
searchbutton = QtGui.QPushButton('Search')
searchbutton.setFixedWidth(80)
searchbutton.clicked.connect(self.onSearch)
SearchBarLayout = QtGui.QHBoxLayout(self)
SearchBarLayout.addWidget(self.valueSearch)
SearchBarLayout.addWidget(searchbutton)
LeftLayout.addLayout(SearchBarLayout)
# -- Webbroser:
self.view = QWebEngineView()
self.channel = QWebChannel(self.view.page())
self.view.page().setWebChannel(self.channel)
self.channel.registerObject("MyApp", self)
file = os.path.join(DirResources, "webs", "main.html")
self.view.page().loadFinished.connect(self.onLoadFinished)
self.view.page().load(QtCore.QUrl.fromLocalFile(file))
LeftLayout.addWidget(self.view)
# -- Latitud y longitud:
self.labelCoordinates = QtGui.QLabel()
self.labelCoordinates.setFixedHeight(21)
LeftLayout.addWidget(self.labelCoordinates)
# Right Widgets:
labelKMZ = QtGui.QLabel()
labelKMZ.setText("Cargar un archivo KMZ/KML:")
self.kmlButton = QtGui.QPushButton()
self.kmlButton.setFixedSize(32, 32)
self.kmlButton.setIcon(QtGui.QIcon(os.path.join(DirIcons, "googleearth.svg")))
widget = QtGui.QWidget(self)
layout = QtGui.QHBoxLayout(widget)
widget.setLayout(layout)
layout.addWidget(labelKMZ)
layout.addWidget(self.kmlButton)
RightLayout.addWidget(widget)
# -----------------------
self.groupbox = QtGui.QGroupBox("Importar datos desde:")
self.groupbox.setCheckable(True)
self.groupbox.setChecked(True)
radio1 = QtGui.QRadioButton("Google Elevation")
radio2 = QtGui.QRadioButton("Nube de Puntos")
radio3 = QtGui.QRadioButton("Datos GPS")
radio1.setChecked(True)
vbox = QtGui.QVBoxLayout(self)
vbox.addWidget(radio1)
vbox.addWidget(radio2)
vbox.addWidget(radio3)
self.groupbox.setLayout(vbox)
RightLayout.addWidget(self.groupbox)
# ------------------------
self.checkboxImportGis = QtGui.QCheckBox("Importar datos GIS")
RightLayout.addWidget(self.checkboxImportGis)
self.checkboxImportSatelitalImagen = QtGui.QCheckBox("Importar Imagen Satelital")
RightLayout.addWidget(self.checkboxImportSatelitalImagen)
verticalSpacer = QtGui.QSpacerItem(20, 48, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
RightLayout.addItem(verticalSpacer)
self.bAccept = QtGui.QPushButton('Accept')
self.bAccept.clicked.connect(self.onAcceptClick)
RightLayout.addWidget(self.bAccept)
# signals/slots
QtCore.QObject.connect(self.kmlButton, QtCore.SIGNAL("clicked()"), self.importKML)
def onLoadFinished(self):
file = os.path.join(DirResources, "webs", "map.js")
frame = self.view.page()
with open(file, 'r') as f:
frame.runJavaScript(f.read())
def onSearch(self):
if self.valueSearch.text() == "":
return
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="http")
location = geolocator.geocode(self.valueSearch.text())
self.valueSearch.setText(location.address)
self.panMap(location.longitude, location.latitude, location.raw['boundingbox'])
def onAcceptClick(self):
frame = self.view.page()
# 1. georeferenciar
frame.runJavaScript(
"MyApp.georeference(drawnItems.getBounds().getCenter().lat, drawnItems.getBounds().getCenter().lng);"
)
# 2. importar todos los elementos dibujados:
frame.runJavaScript(
"var data = drawnItems.toGeoJSON();"
"MyApp.shapes(JSON.stringify(data));"
)
self.close()
@QtCore.Slot(float, float)
def onMapMove(self, lat, lng):
from lib.projection import latlon_to_utm
self.lat = lat
self.lon = lng
easting, northing, zone_number, zone_letter = latlon_to_utm(lat, lng)
self.labelCoordinates.setText('Longitud: {:.5f}, Latitud: {:.5f}'.format(lng, lat) +
' | UTM: ' + str(zone_number) + zone_letter +
', {:.5f}m E, {:.5f}m N'.format(easting, northing))
@QtCore.Slot(float, float, float, float, int)
def onMapZoom(self, minLat, minLon, maxLat, maxLon, zoom):
self.minLat = min([minLat, maxLat])
self.maxLat = max([minLat, maxLat])
self.minLon = min([minLon, maxLon])
self.maxLon = max([minLon, maxLon])
self.zoom = zoom
@QtCore.Slot(float, float)
def georeference(self, lat, lng):
import PVPlantSite
from geopy.geocoders import Nominatim
self.georeference_coordinates['lat'] = lat
self.georeference_coordinates['lon'] = lng
Site = PVPlantSite.get(create=True)
Site.Proxy.setLatLon(lat, lng)
geolocator = Nominatim(user_agent="http")
location = geolocator.reverse('{:.5f}, {:.5f}'.format(lat, lng))
if location:
if location.raw["address"].get("road"):
str = location.raw["address"]["road"]
if location.raw["address"].get("house_number"):
str += ' ({0})'.format(location.raw["address"]["house_number"])
Site.Address = str
if location.raw["address"].get("city"):
Site.City = location.raw["address"]["city"]
if location.raw["address"].get("postcode"):
Site.PostalCode = location.raw["address"]["postcode"]
if location.raw["address"].get("address"):
Site.Region = '{0}'.format(location.raw["address"]["province"])
if location.raw["address"].get("state"):
if Site.Region != "":
Site.Region += " - "
Site.Region += '{0}'.format(location.raw["address"]["state"])
Site.Country = location.raw["address"]["country"]
@QtCore.Slot(str)
def shapes(self, drawnItems):
import geojson
import PVPlantImportGrid as ImportElevation
import Draft
import PVPlantSite
Site = PVPlantSite.get()
offset = FreeCAD.Vector(0, 0, 0)
if not (self.lat is None or self.lon is None):
offset = FreeCAD.Vector(Site.Origin)
offset.z = 0
items = geojson.loads(drawnItems)
for item in items['features']:
if item['geometry']['type'] == "Point": # 1. if the feature is a Point or Circle:
coord = item['geometry']['coordinates']
point = ImportElevation.getElevationFromOE([[coord[1], coord[0]],])
c = FreeCAD.Vector(point[0][0], point[0][1], point[0][2]).sub(offset)
if item['properties'].get('radius'):
r = round(item['properties']['radius'] * 1000, 0)
p = FreeCAD.Placement()
p.Base = c
obj = Draft.makeCircle(r, placement=p, face=False)
else:
obj = Draft.make_point(c * 1000, color=(0.5, 0.3, 0.6), point_size=10)
else: # 2. if the feature is a Polygon or Line:
cw = False
name = "Línea"
lp = item['geometry']['coordinates']
if item['geometry']['type'] == "Polygon":
cw = True
name = "Area"
lp = item['geometry']['coordinates'][0]
pts = [[cords[1], cords[0]] for cords in lp]
tmp = ImportElevation.getElevationFromOE(pts)
pts = [p.sub(offset) for p in tmp]
obj = Draft.makeWire(pts, closed=cw, face=False)
obj.Label = name
Draft.autogroup(obj)
if item['properties'].get('name'):
obj.Label = item['properties']['name']
if self.checkboxImportGis.isChecked():
self.getDataFromOSM(self.minLat, self.minLon, self.maxLat, self.maxLon)
if self.checkboxImportSatelitalImagen.isChecked():
from lib.projection import latlon_to_utm
s_lat = self.minLat
s_lon = self.minLon
n_lat = self.maxLat
n_lon = self.maxLon
# Obtener puntos UTM para las esquinas y el punto de referencia
points = [
[s_lat, s_lon], # Suroeste
[n_lat, n_lon], # Noreste
[self.georeference_coordinates['lat'], self.georeference_coordinates['lon']] # Punto de referencia
]
utm_points = ImportElevation.getElevationFromOE(points)
if not utm_points or len(utm_points) < 3:
FreeCAD.Console.PrintError("Error obteniendo elevaciones para las esquinas y referencia\n")
return
sw_utm, ne_utm, ref_utm = utm_points
# Descargar imagen satelital
from lib.GoogleSatelitalImageDownload import GoogleMapDownloader
downloader = GoogleMapDownloader(
zoom=self.zoom,
layer='raw_satellite'
)
img = downloader.generateImage(
sw_lat=s_lat,
sw_lng=s_lon,
ne_lat=n_lat,
ne_lng=n_lon
)
# Guardar imagen
doc_path = os.path.dirname(FreeCAD.ActiveDocument.FileName) if FreeCAD.ActiveDocument.FileName else ""
if not doc_path:
doc_path = FreeCAD.ConfigGet("UserAppData")
filename = os.path.join(doc_path, "background.jpeg")
img.save(filename)
# Calcular dimensiones reales en metros
width_m = ne_utm.x - sw_utm.x
height_m = ne_utm.y - sw_utm.y
# Calcular posición relativa del punto de referencia dentro de la imagen
rel_x = (ref_utm.x - sw_utm.x) / width_m if width_m != 0 else 0.5
rel_y = (ref_utm.y - sw_utm.y) / height_m if height_m != 0 else 0.5
# Crear objeto de imagen en FreeCAD
doc = FreeCAD.ActiveDocument
img_obj = doc.addObject('Image::ImagePlane', 'Background')
img_obj.ImageFile = filename
img_obj.Label = 'Background'
# FreeCAD trabaja en mm
img_obj.XSize = width_m * 1000
img_obj.YSize = height_m * 1000
# Posicionar para que el punto de referencia esté en (0,0,0)
img_obj.Placement.Base = FreeCAD.Vector(
-rel_x * width_m * 1000,
-rel_y * height_m * 1000,
0
)
doc.recompute()
def getDataFromOSM(self, min_lat, min_lon, max_lat, max_lon):
import Importer.importOSM as importOSM
import PVPlantSite
site = PVPlantSite.get()
offset = FreeCAD.Vector(0, 0, 0)
if not (self.lat is None or self.lon is None):
offset = FreeCAD.Vector(site.Origin)
offset.z = 0
importer = importOSM.OSMImporter(offset)
osm_data = importer.get_osm_data(f"{min_lat},{min_lon},{max_lat},{max_lon}")
importer.process_osm_data(osm_data)
def panMap(self, lng, lat, geometry=None):
frame = self.view.page()
if not geometry or len(geometry) < 4:
command = f'map.panTo(L.latLng({lat}, {lng}));'
else:
try:
southwest = f"{float(geometry[1])}, {float(geometry[0])}"
northeast = f"{float(geometry[3])}, {float(geometry[2])}"
command = f'map.panTo(L.latLng({lat}, {lng}));'
command += f'map.fitBounds(L.latLngBounds([{southwest}], [{northeast}]));'
except (IndexError, ValueError, TypeError) as e:
print(f"Error en geometry: {str(e)}")
command = f'map.panTo(L.latLng({lat}, {lng}));'
frame.runJavaScript(command)
def importKML(self):
file = QtGui.QFileDialog.getOpenFileName(None, "FileDialog", "", "Google Earth (*.kml *.kmz)")[0]
from lib.kml2geojson import kmz_convert
layers = kmz_convert(file, "", )
frame = self.view.page()
for layer in layers:
command = "var geoJsonLayer = L.geoJSON({0}); drawnItems.addLayer(geoJsonLayer); map.fitBounds(geoJsonLayer.getBounds());".format( layer)
frame.runJavaScript(command)
class CommandPVPlantGeoreferencing:
def GetResources(self):
return {'Pixmap': str(os.path.join(DirIcons, "Location.svg")),
'Accel': "G, R",
'MenuText': QT_TRANSLATE_NOOP("Georeferencing","Georeferencing"),
'ToolTip': QT_TRANSLATE_NOOP("Georeferencing","Referenciar el lugar")}
def Activated(self):
self.form = MapWindow()
self.form.show()
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
-208
View File
@@ -1,208 +0,0 @@
# /**********************************************************************
# * *
# * 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, Draft, math, datetime
import ArchSite
if FreeCAD.GuiUp:
import FreeCADGui
from DraftTools import translate
from PySide.QtCore import QT_TRANSLATE_NOOP
from pivy import coin
else:
def translate(ctxt, txt):
return txt
def QT_TRANSLATE_NOOP(ctxt, txt):
return txt
import os
from PVPlantResources import DirIcons as DirIcons
zone_list = ["Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9", "Z10", "Z11", "Z12",
"Z13", "Z14", "Z15", "Z16", "Z17", "Z18", "Z19", "Z20", "Z21", "Z22", "Z23", "Z24",
"Z25", "Z26", "Z27", "Z28", "Z29", "Z30", "Z31", "Z32", "Z33", "Z34", "Z35", "Z36",
"Z37", "Z38", "Z39", "Z40", "Z41", "Z42", "Z43", "Z44", "Z45", "Z46", "Z47", "Z48",
"Z49", "Z50", "Z51", "Z52", "Z53", "Z54", "Z55", "Z56", "Z57", "Z58", "Z59", "Z60"]
def get(origin=FreeCAD.Vector(0, 0, 0), create=False):
obj = FreeCAD.ActiveDocument.getObject('Site')
if obj:
if obj.Origin == FreeCAD.Vector(0, 0, 0):
obj.Origin = origin
return obj
if not obj and create:
obj = makePVPlantSite()
return obj
def PartToWire(part):
import Part, Draft
PointList = []
edges = Part.__sortEdges__(part.Shape.Edges)
for edge in edges:
PointList.append(edge.Vertexes[0].Point)
PointList.append(edges[-1].Vertexes[-1].Point)
Draft.makeWire(PointList, closed=True, face=None, support=None)
def projectWireOnMesh(Boundary, Mesh):
import Draft
import MeshPart as mp
plist = mp.projectShapeOnMesh(Boundary.Shape, Mesh, FreeCAD.Vector(0, 0, 1))
PointList = []
for pl in plist:
PointList += pl
Draft.makeWire(PointList, closed=True, face=None, support=None)
FreeCAD.activeDocument().recompute()
def makePVPlantSite():
def createGroup(father, groupname, type=None):
group = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", groupname)
group.Label = groupname
father.addObject(group)
return group
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "Site")
_PVPlantSite(obj)
if FreeCAD.GuiUp:
_ViewProviderSite(obj.ViewObject)
group = createGroup(obj, "CivilGroup")
group1 = createGroup(group, "Areas")
createGroup(group1, "Boundaries")
createGroup(group1, "CadastralPlots")
createGroup(group1, "Exclusions")
createGroup(group1, "FrameZones")
createGroup(group1, "Offsets")
createGroup(group1, "Plots")
createGroup(group, "Drains")
createGroup(group, "Earthworks")
createGroup(group, "Fences")
createGroup(group, "Foundations")
createGroup(group, "Pads")
createGroup(group, "Points")
createGroup(group, "Roads")
createGroup(group, "Trenches")
group = createGroup(obj, "ElectricalGroup")
createGroup(group, "StringInverters")
createGroup(group, "CentralInverter")
group1 = createGroup(group, "AC")
createGroup(group1, "CableAC")
group1 = createGroup(group, "DC")
createGroup(group1, "CableDC")
createGroup(group1, "StringsSetup")
createGroup(group1, "Strings")
createGroup(group1, "StringsBoxes")
group = createGroup(obj, "MechanicalGroup")
createGroup(group, "FramesSetups")
createGroup(group, "Frames")
group = createGroup(obj, "Environment")
createGroup(group, "Vegetation")
return obj
class _PVPlantSite(ArchSite._Site):
"The Site object"
def __init__(self, obj):
ArchSite._Site.__init__(self, obj)
self.obj = obj
self.Type = "Site"
obj.Proxy = self
obj.IfcType = "Site"
obj.setEditorMode("IfcType", 1)
def setProperties(self, obj):
ArchSite._Site.setProperties(self, obj)
obj.addProperty("App::PropertyLink", "Boundary", "PVPlant", "Boundary of land")
obj.addProperty("App::PropertyLinkList", "Frames", "PVPlant", "Frames templates")
obj.addProperty("App::PropertyEnumeration", "UtmZone", "PVPlant", "UTM zone").UtmZone = zone_list
obj.addProperty("App::PropertyVector", "Origin", "PVPlant", "Origin point.").Origin = (0, 0, 0)
def onDocumentRestored(self, obj):
self.obj = obj
self.Type = "Site"
obj.Proxy = self
def onChanged(self, obj, prop):
ArchSite._Site.onChanged(self, obj, prop)
if (prop == "Terrain") or (prop == "Boundary"):
if obj.Terrain and obj.Boundary:
print("Calcular 3D boundary")
if prop == "UtmZone":
node = self.get_geoorigin()
zone = obj.getPropertyByName("UtmZone")
geo_system = ["UTM", zone, "FLAT"]
node.geoSystem.setValues(geo_system)
if prop == "Origin":
node = self.get_geoorigin()
origin = obj.getPropertyByName("Origin")
node.geoCoords.setValue(origin.x, origin.y, 0)
obj.Placement.Base = obj.getPropertyByName(prop)
def execute(self, obj):
ArchSite._Site.execute(self, obj)
def computeAreas(self, obj):
ArchSite._Site.computeAreas(self, obj)
def __getstate__(self):
node = self.get_geoorigin()
system = node.geoSystem.getValues()
x, y, z = node.geoCoords.getValue().getValue()
return system, [x, y, z]
def __setstate__(self, state):
if state:
system = state[0]
origin = state[1]
node = self.get_geoorigin()
node.geoSystem.setValues(system)
node.geoCoords.setValue(origin[0], origin[1], 0)
def get_geoorigin(self):
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
node = sg.getChild(0)
if not isinstance(node, coin.SoGeoOrigin):
node = coin.SoGeoOrigin()
sg.insertChild(node, 0)
return node
def setLatLon(self, lat, lon):
from lib.projection import latlon_to_utm
import PVPlantImportGrid
easting, northing, zone_number, zone_letter = latlon_to_utm(lat, lon)
self.obj.UtmZone = zone_list[zone_number - 1]
point = PVPlantImportGrid.getElevationFromOE([[lat, lon]])
self.obj.Origin = FreeCAD.Vector(point[0].x, point[0].y, point[0].z)
self.obj.Latitude = lat
self.obj.Longitude = lon
self.obj.Elevation = point[0].z
from PVPlant.core.view_provider import ViewProviderSite as _ViewProviderSite
-353
View File
@@ -1,353 +0,0 @@
# /**********************************************************************
# * *
# * 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, math, datetime
from pivy import coin
def makeSolarDiagram(longitude, latitude, scale=1, complete=False, tz=None):
"""makeSolarDiagram(longitude,latitude,[scale,complete,tz]):
returns a solar diagram as a pivy node. If complete is
True, the 12 months are drawn. Tz is the timezone related to
UTC (ex: -3 = UTC-3)"""
oldversion = False
ladybug = False
try:
import ladybug
from ladybug import location
from ladybug import sunpath
except:
ladybug = False
try:
import pysolar
except:
try:
import Pysolar as pysolar
except:
FreeCAD.Console.PrintError("The pysolar module was not found. Unable to generate solar diagrams\n")
return None
else:
oldversion = True
if tz:
tz = datetime.timezone(datetime.timedelta(hours=-3))
else:
tz = datetime.timezone.utc
else:
loc = ladybug.location.Location(latitude=latitude, longitude=longitude, time_zone=tz)
sunpath = ladybug.sunpath.Sunpath.from_location(loc)
if not scale:
return None
circles = []
sunpaths = []
hourpaths = []
circlepos = []
hourpos = []
import Part
for i in range(1, 9):
circles.append(Part.makeCircle(scale * (i / 8.0)))
for ad in range(0, 360, 15):
a = math.radians(ad)
p1 = FreeCAD.Vector(math.cos(a) * scale, math.sin(a) * scale, 0)
p2 = FreeCAD.Vector(math.cos(a) * scale * 0.125, math.sin(a) * scale * 0.125, 0)
p3 = FreeCAD.Vector(math.cos(a) * scale * 1.08, math.sin(a) * scale * 1.08, 0)
circles.append(Part.LineSegment(p1, p2).toShape())
circlepos.append((ad, p3))
year = datetime.datetime.now().year
hpts = [[] for i in range(24)]
m = [(6, 21), (7, 21), (8, 21), (9, 21), (10, 21), (11, 21), (12, 21)]
if complete:
m.extend([(1, 21), (2, 21), (3, 21), (4, 21), (5, 21)])
for i, d in enumerate(m):
pts = []
for h in range(24):
if ladybug:
sun = sunpath.calculate_sun(month=d[0], day=d[1], hour=h)
alt = math.radians(sun.altitude)
az = 90 + sun.azimuth
elif oldversion:
dt = datetime.datetime(year, d[0], d[1], h)
alt = math.radians(pysolar.solar.GetAltitudeFast(latitude, longitude, dt))
az = pysolar.solar.GetAzimuth(latitude, longitude, dt)
az = -90 + az
else:
dt = datetime.datetime(year, d[0], d[1], h, tzinfo=tz)
alt = math.radians(pysolar.solar.get_altitude_fast(latitude, longitude, dt))
az = pysolar.solar.get_azimuth(latitude, longitude, dt)
az = 90 + az
if az < 0:
az = 360 + az
az = math.radians(az)
zc = math.sin(alt) * scale
ic = math.cos(alt) * scale
xc = math.cos(az) * ic
yc = math.sin(az) * ic
p = FreeCAD.Vector(xc, yc, zc)
pts.append(p)
hpts[h].append(p)
if i in [0, 6]:
ep = FreeCAD.Vector(p)
ep.multiply(1.08)
if ep.z >= 0:
if not oldversion:
h = 24 - h
if h == 12:
if i == 0:
h = "SUMMER"
else:
h = "WINTER"
if latitude < 0:
if h == "SUMMER":
h = "WINTER"
else:
h = "SUMMER"
hourpos.append((h, ep))
if i < 7:
sunpaths.append(Part.makePolygon(pts))
for h in hpts:
if complete:
h.append(h[0])
hourpaths.append(Part.makePolygon(h))
sz = 2.1 * scale
cube = Part.makeBox(sz, sz, sz)
cube.translate(FreeCAD.Vector(-sz / 2, -sz / 2, -sz))
sunpaths = [sp.cut(cube) for sp in sunpaths]
hourpaths = [hp.cut(cube) for hp in hourpaths]
ts = 0.005 * scale
mastersep = coin.SoSeparator()
circlesep = coin.SoSeparator()
numsep = coin.SoSeparator()
pathsep = coin.SoSeparator()
hoursep = coin.SoSeparator()
hournumsep = coin.SoSeparator()
mastersep.addChild(circlesep)
mastersep.addChild(numsep)
mastersep.addChild(pathsep)
mastersep.addChild(hoursep)
for item in circles:
circlesep.addChild(toNode(item))
for item in sunpaths:
for w in item.Edges:
pathsep.addChild(toNode(w))
for item in hourpaths:
for w in item.Edges:
hoursep.addChild(toNode(w))
for p in circlepos:
text = coin.SoText2()
s = p[0] - 90
s = -s
if s > 360:
s = s - 360
if s < 0:
s = 360 + s
if s == 0:
s = "N"
elif s == 90:
s = "E"
elif s == 180:
s = "S"
elif s == 270:
s = "W"
else:
s = str(s)
text.string = s
text.justification = coin.SoText2.CENTER
coords = coin.SoTransform()
coords.translation.setValue([p[1].x, p[1].y, p[1].z])
coords.scaleFactor.setValue([ts, ts, ts])
item = coin.SoSeparator()
item.addChild(coords)
item.addChild(text)
numsep.addChild(item)
for p in hourpos:
text = coin.SoText2()
s = str(p[0])
text.string = s
text.justification = coin.SoText2.CENTER
coords = coin.SoTransform()
coords.translation.setValue([p[1].x, p[1].y, p[1].z])
coords.scaleFactor.setValue([ts, ts, ts])
item = coin.SoSeparator()
item.addChild(coords)
item.addChild(text)
numsep.addChild(item)
return mastersep
def makeWindRose(epwfile, scale=1, sectors=24):
try:
import ladybug
from ladybug import epw
except:
FreeCAD.Console.PrintError("The ladybug module was not found. Unable to generate solar diagrams\n")
return None
if not epwfile:
FreeCAD.Console.PrintWarning("No EPW file, unable to generate wind rose.\n")
return None
epw_data = ladybug.epw.EPW(epwfile)
baseangle = 360 / sectors
sectorangles = [i * baseangle for i in range(sectors)]
basebissect = baseangle / 2
angles = [basebissect]
for i in range(1, sectors):
angles.append(angles[-1] + baseangle)
windsbysector = [0 for i in range(sectors)]
for hour in epw_data.wind_direction:
sector = min(angles, key=lambda x: abs(x - hour))
sectorindex = angles.index(sector)
windsbysector[sectorindex] = windsbysector[sectorindex] + 1
maxwind = max(windsbysector)
windsbysector = [wind / maxwind for wind in windsbysector]
vectors = []
dividers = []
for i in range(sectors):
angle = math.radians(90 + angles[i])
x = math.cos(angle) * windsbysector[i] * scale
y = math.sin(angle) * windsbysector[i] * scale
vectors.append(FreeCAD.Vector(x, y, 0))
secangle = math.radians(90 + sectorangles[i])
x = math.cos(secangle) * scale
y = math.sin(secangle) * scale
dividers.append(FreeCAD.Vector(x, y, 0))
vectors.append(vectors[0])
import Part
masternode = coin.SoSeparator()
for r in (0.25, 0.5, 0.75, 1.0):
c = Part.makeCircle(r * scale)
masternode.addChild(toNode(c))
for divider in dividers:
l = Part.makeLine(FreeCAD.Vector(), divider)
masternode.addChild(toNode(l))
ds = coin.SoDrawStyle()
ds.lineWidth = 2.0
masternode.addChild(ds)
d = Part.makePolygon(vectors)
masternode.addChild(toNode(d))
return masternode
# Values in mm
COMPASS_POINTER_LENGTH = 1000
COMPASS_POINTER_WIDTH = 100
class Compass(object):
def __init__(self):
self.rootNode = self.setupCoin()
def show(self):
self.compassswitch.whichChild = coin.SO_SWITCH_ALL
def hide(self):
self.compassswitch.whichChild = coin.SO_SWITCH_NONE
def rotate(self, angleInDegrees):
self.transform.rotation.setValue(
coin.SbVec3f(0, 0, 1), math.radians(angleInDegrees))
def locate(self, x, y, z):
self.transform.translation.setValue(x, y, z)
def scale(self, area):
s = round(max(math.sqrt(area.getValueAs("m^2").Value) / 10, 1))
self.transform.scaleFactor.setValue(coin.SbVec3f(s, s, 1))
def setupCoin(self):
compasssep = coin.SoSeparator()
self.transform = coin.SoTransform()
darkNorthMaterial = coin.SoMaterial()
darkNorthMaterial.diffuseColor.set1Value(0, 0.5, 0, 0)
lightNorthMaterial = coin.SoMaterial()
lightNorthMaterial.diffuseColor.set1Value(0, 0.9, 0, 0)
darkGreyMaterial = coin.SoMaterial()
darkGreyMaterial.diffuseColor.set1Value(0, 0.9, 0.9, 0.9)
lightGreyMaterial = coin.SoMaterial()
lightGreyMaterial.diffuseColor.set1Value(0, 0.5, 0.5, 0.5)
coords = self.buildCoordinates()
lightColorFaceset = coin.SoIndexedFaceSet()
lightColorCoordinateIndex = [4, 5, 6, -1, 8, 9, 10, -1, 12, 13, 14, -1]
lightColorFaceset.coordIndex.setValues(0, len(lightColorCoordinateIndex), lightColorCoordinateIndex)
darkColorFaceset = coin.SoIndexedFaceSet()
darkColorCoordinateIndex = [6, 7, 4, -1, 10, 11, 8, -1, 14, 15, 12, -1]
darkColorFaceset.coordIndex.setValues(0, len(darkColorCoordinateIndex), darkColorCoordinateIndex)
lightNorthFaceset = coin.SoIndexedFaceSet()
lightNorthCoordinateIndex = [2, 3, 0, -1]
lightNorthFaceset.coordIndex.setValues(0, len(lightNorthCoordinateIndex), lightNorthCoordinateIndex)
darkNorthFaceset = coin.SoIndexedFaceSet()
darkNorthCoordinateIndex = [0, 1, 2, -1]
darkNorthFaceset.coordIndex.setValues(0, len(darkNorthCoordinateIndex), darkNorthCoordinateIndex)
self.compassswitch = coin.SoSwitch()
self.compassswitch.whichChild = coin.SO_SWITCH_NONE
self.compassswitch.addChild(compasssep)
lightGreySeparator = coin.SoSeparator()
lightGreySeparator.addChild(lightGreyMaterial)
lightGreySeparator.addChild(lightColorFaceset)
darkGreySeparator = coin.SoSeparator()
darkGreySeparator.addChild(darkGreyMaterial)
darkGreySeparator.addChild(darkColorFaceset)
lightNorthSeparator = coin.SoSeparator()
lightNorthSeparator.addChild(lightNorthMaterial)
lightNorthSeparator.addChild(lightNorthFaceset)
darkNorthSeparator = coin.SoSeparator()
darkNorthSeparator.addChild(darkNorthMaterial)
darkNorthSeparator.addChild(darkNorthFaceset)
compasssep.addChild(coords)
compasssep.addChild(self.transform)
compasssep.addChild(lightGreySeparator)
compasssep.addChild(darkGreySeparator)
compasssep.addChild(lightNorthSeparator)
compasssep.addChild(darkNorthSeparator)
return self.compassswitch
def buildCoordinates(self):
coords = coin.SoCoordinate3()
coords.point.set1Value(0, 0, 0, 0)
coords.point.set1Value(1, COMPASS_POINTER_WIDTH, COMPASS_POINTER_WIDTH, 0)
coords.point.set1Value(2, 0, COMPASS_POINTER_LENGTH, 0)
coords.point.set1Value(3, -COMPASS_POINTER_WIDTH, COMPASS_POINTER_WIDTH, 0)
coords.point.set1Value(4, 0, 0, 0)
coords.point.set1Value(5, COMPASS_POINTER_WIDTH, -COMPASS_POINTER_WIDTH, 0)
coords.point.set1Value(6, COMPASS_POINTER_LENGTH, 0, 0)
coords.point.set1Value(7, COMPASS_POINTER_WIDTH, COMPASS_POINTER_WIDTH, 0)
coords.point.set1Value(8, 0, 0, 0)
coords.point.set1Value(9, -COMPASS_POINTER_WIDTH, -COMPASS_POINTER_WIDTH, 0)
coords.point.set1Value(10, 0, -COMPASS_POINTER_LENGTH, 0)
coords.point.set1Value(11, COMPASS_POINTER_WIDTH, -COMPASS_POINTER_WIDTH, 0)
coords.point.set1Value(12, 0, 0, 0)
coords.point.set1Value(13, -COMPASS_POINTER_WIDTH, COMPASS_POINTER_WIDTH, 0)
coords.point.set1Value(14, -COMPASS_POINTER_LENGTH, 0, 0)
coords.point.set1Value(15, -COMPASS_POINTER_WIDTH, -COMPASS_POINTER_WIDTH, 0)
return coords
-283
View File
@@ -1,283 +0,0 @@
# /**********************************************************************
# * *
# * 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, math
from pivy import coin
if FreeCAD.GuiUp:
import FreeCADGui
from DraftTools import translate
from PySide.QtCore import QT_TRANSLATE_NOOP
from PVPlant.core.solar_compass import makeSolarDiagram, makeWindRose, Compass
class ViewProviderSite(object):
"""View Provider for the Site object. Handles solar diagram, wind rose, compass and true north."""
def __init__(self, vobj):
vobj.Proxy = self
vobj.addExtension("Gui::ViewProviderGroupExtensionPython", self)
self.setProperties(vobj)
def setProperties(self, vobj):
from PVPlantResources import DirIcons as DirIcons
pl = vobj.PropertiesList
if not "WindRose" in pl:
vobj.addProperty("App::PropertyBool", "WindRose", "Site",
QT_TRANSLATE_NOOP("App::Property", "Show wind rose diagram or not. Uses solar diagram scale. Needs Ladybug module"))
if not "SolarDiagram" in pl:
vobj.addProperty("App::PropertyBool", "SolarDiagram", "Site",
QT_TRANSLATE_NOOP("App::Property", "Show solar diagram or not"))
if not "SolarDiagramScale" in pl:
vobj.addProperty("App::PropertyFloat", "SolarDiagramScale", "Site",
QT_TRANSLATE_NOOP("App::Property", "The scale of the solar diagram"))
vobj.SolarDiagramScale = 1
if not "SolarDiagramPosition" in pl:
vobj.addProperty("App::PropertyVector", "SolarDiagramPosition", "Site",
QT_TRANSLATE_NOOP("App::Property", "The position of the solar diagram"))
if not "SolarDiagramColor" in pl:
vobj.addProperty("App::PropertyColor", "SolarDiagramColor", "Site",
QT_TRANSLATE_NOOP("App::Property", "The color of the solar diagram"))
vobj.SolarDiagramColor = (0.16, 0.16, 0.25)
if not "Orientation" in pl:
vobj.addProperty("App::PropertyEnumeration", "Orientation", "Site",
QT_TRANSLATE_NOOP("App::Property", "When set to 'True North' the whole geometry will be rotated to match the true north of this site"))
vobj.Orientation = ["Project North", "True North"]
vobj.Orientation = "Project North"
if not "Compass" in pl:
vobj.addProperty("App::PropertyBool", "Compass", "Compass",
QT_TRANSLATE_NOOP("App::Property", "Show compass or not"))
if not "CompassRotation" in pl:
vobj.addProperty("App::PropertyAngle", "CompassRotation", "Compass",
QT_TRANSLATE_NOOP("App::Property", "The rotation of the Compass relative to the Site"))
if not "CompassPosition" in pl:
vobj.addProperty("App::PropertyVector", "CompassPosition", "Compass",
QT_TRANSLATE_NOOP("App::Property", "The position of the Compass relative to the Site placement"))
if not "UpdateDeclination" in pl:
vobj.addProperty("App::PropertyBool", "UpdateDeclination", "Compass",
QT_TRANSLATE_NOOP("App::Property", "Update the Declination value based on the compass rotation"))
def onDocumentRestored(self, vobj):
self.setProperties(vobj)
def getIcon(self):
from PVPlantResources import DirIcons as DirIcons
return str(os.path.join(DirIcons, "solar-panel.svg"))
def claimChildren(self):
objs = []
if hasattr(self, "Object"):
objs = self.Object.Group + [self.Object.Terrain]
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch")
if hasattr(self.Object, "Additions") and prefs.GetBool("swallowAdditions", True):
objs.extend(self.Object.Additions)
if hasattr(self.Object, "Subtractions") and prefs.GetBool("swallowSubtractions", True):
objs.extend(self.Object.Subtractions)
return objs
def setEdit(self, vobj, mode):
if (mode == 0) and hasattr(self, "Object"):
import ArchComponent
taskd = ArchComponent.ComponentTaskPanel()
taskd.obj = self.Object
taskd.update()
FreeCADGui.Control.showDialog(taskd)
return True
return False
def unsetEdit(self, vobj, mode):
FreeCADGui.Control.closeDialog()
return False
def attach(self, vobj):
self.Object = vobj.Object
basesep = coin.SoSeparator()
vobj.Annotation.addChild(basesep)
self.color = coin.SoBaseColor()
self.coords = coin.SoTransform()
basesep.addChild(self.coords)
basesep.addChild(self.color)
self.diagramsep = coin.SoSeparator()
self.diagramswitch = coin.SoSwitch()
self.diagramswitch.whichChild = -1
self.diagramswitch.addChild(self.diagramsep)
basesep.addChild(self.diagramswitch)
self.windrosesep = coin.SoSeparator()
self.windroseswitch = coin.SoSwitch()
self.windroseswitch.whichChild = -1
self.windroseswitch.addChild(self.windrosesep)
basesep.addChild(self.windroseswitch)
self.compass = Compass()
self.updateCompassVisibility(vobj)
self.updateCompassScale(vobj)
self.rotateCompass(vobj)
vobj.Annotation.addChild(self.compass.rootNode)
def updateData(self, obj, prop):
if prop in ["Longitude", "Latitude"]:
self.onChanged(obj.ViewObject, "SolarDiagram")
elif prop == "Declination":
self.onChanged(obj.ViewObject, "SolarDiagramPosition")
self.updateTrueNorthRotation()
elif prop == "Terrain":
self.updateCompassLocation(obj.ViewObject)
elif prop == "Placement":
self.updateCompassLocation(obj.ViewObject)
self.updateDeclination(obj.ViewObject)
elif prop == "ProjectedArea":
self.updateCompassScale(obj.ViewObject)
def onChanged(self, vobj, prop):
if prop == "SolarDiagramPosition":
if hasattr(vobj, "SolarDiagramPosition"):
p = vobj.SolarDiagramPosition
self.coords.translation.setValue([p.x, p.y, p.z])
if hasattr(vobj.Object, "Declination"):
self.coords.rotation.setValue(coin.SbVec3f((0, 0, 1)), math.radians(vobj.Object.Declination.Value))
elif prop == "SolarDiagramColor":
if hasattr(vobj, "SolarDiagramColor"):
l = vobj.SolarDiagramColor
self.color.rgb.setValue([l[0], l[1], l[2]])
elif "SolarDiagram" in prop:
if hasattr(self, "diagramnode"):
self.diagramsep.removeChild(self.diagramnode)
del self.diagramnode
if hasattr(vobj, "SolarDiagram") and hasattr(vobj, "SolarDiagramScale"):
if vobj.SolarDiagram:
tz = 0
if hasattr(vobj.Object, "TimeZone"):
tz = vobj.Object.TimeZone
self.diagramnode = makeSolarDiagram(vobj.Object.Longitude, vobj.Object.Latitude,
vobj.SolarDiagramScale, tz=tz)
if self.diagramnode:
self.diagramsep.addChild(self.diagramnode)
self.diagramswitch.whichChild = 0
else:
del self.diagramnode
else:
self.diagramswitch.whichChild = -1
elif prop == "WindRose":
if hasattr(self, "windrosenode"):
del self.windrosenode
if hasattr(vobj, "WindRose"):
if vobj.WindRose:
if hasattr(vobj.Object, "EPWFile") and vobj.Object.EPWFile:
try:
import ladybug
except:
pass
else:
self.windrosenode = makeWindRose(vobj.Object.EPWFile, vobj.SolarDiagramScale)
if self.windrosenode:
self.windrosesep.addChild(self.windrosenode)
self.windroseswitch.whichChild = 0
else:
del self.windrosenode
else:
self.windroseswitch.whichChild = -1
elif prop == 'Visibility':
if vobj.Visibility:
self.updateCompassVisibility(self.Object)
else:
self.compass.hide()
elif prop == 'Orientation':
if vobj.Orientation == 'True North':
self.addTrueNorthRotation()
else:
self.removeTrueNorthRotation()
elif prop == "UpdateDeclination":
self.updateDeclination(vobj)
elif prop == "Compass":
self.updateCompassVisibility(vobj)
elif prop == "CompassRotation":
self.updateDeclination(vobj)
self.rotateCompass(vobj)
elif prop == "CompassPosition":
self.updateCompassLocation(vobj)
def updateDeclination(self, vobj):
if not hasattr(vobj, 'UpdateDeclination') or not vobj.UpdateDeclination:
return
compassRotation = vobj.CompassRotation.Value
siteRotation = math.degrees(vobj.Object.Placement.Rotation.Angle)
vobj.Object.Declination = compassRotation + siteRotation
def addTrueNorthRotation(self):
if hasattr(self, 'trueNorthRotation') and self.trueNorthRotation is not None:
return
self.trueNorthRotation = coin.SoTransform()
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
sg.insertChild(self.trueNorthRotation, 0)
self.updateTrueNorthRotation()
def removeTrueNorthRotation(self):
if hasattr(self, 'trueNorthRotation') and self.trueNorthRotation is not None:
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
sg.removeChild(self.trueNorthRotation)
self.trueNorthRotation = None
def updateTrueNorthRotation(self):
if hasattr(self, 'trueNorthRotation') and self.trueNorthRotation is not None:
angle = self.Object.Declination.Value
self.trueNorthRotation.rotation.setValue(coin.SbVec3f(0, 0, 1), math.radians(-angle))
def updateCompassVisibility(self, vobj):
if not hasattr(self, 'compass'):
return
show = hasattr(vobj, 'Compass') and vobj.Compass
if show:
self.compass.show()
else:
self.compass.hide()
def rotateCompass(self, vobj):
if not hasattr(self, 'compass'):
return
if hasattr(vobj, 'CompassRotation'):
self.compass.rotate(vobj.CompassRotation.Value)
def updateCompassLocation(self, vobj):
if not hasattr(self, 'compass'):
return
if not vobj.Object.Shape:
return
boundBox = vobj.Object.Shape.BoundBox
pos = vobj.Object.Placement.Base
x = 0
y = 0
if hasattr(vobj, "CompassPosition"):
x = vobj.CompassPosition.x
y = vobj.CompassPosition.y
z = boundBox.ZMax = pos.z
self.compass.locate(x, y, z + 1000)
def updateCompassScale(self, vobj):
if not hasattr(self, 'compass'):
return
self.compass.scale(vobj.Object.ProjectedArea)
def __getstate__(self):
return None
def __setstate__(self, state):
return None
View File
-671
View File
@@ -1,671 +0,0 @@
# /**********************************************************************
# * *
# * 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 json
import urllib.request
import Draft
import FreeCAD
import FreeCADGui
from PySide import QtCore, QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
import os
from PVPlantResources import DirIcons as DirIcons
import PVPlantSite
def get_elevation_from_oe(coordinates):
"""Obtiene elevaciones de Open-Elevation API y devuelve vectores FreeCAD en coordenadas UTM.
Args:
coordinates (list): Lista de tuplas con coordenadas (latitud, longitud)
Returns:
list: Lista de vectores FreeCAD con coordenadas UTM y elevación (en milímetros)
o lista vacía en caso de error.
"""
if not coordinates:
return []
import requests
from lib.projection import latlon_to_utm
from requests.exceptions import RequestException
locations = "|".join([f"{lat:.6f},{lon:.6f}" for lat, lon in coordinates])
try:
response = requests.get(
url="https://api.open-elevation.com/api/v1/lookup",
params={'locations': locations},
timeout=20,
verify=True
)
response.raise_for_status()
except RequestException as e:
print(f"Error en la solicitud: {str(e)}")
return []
try:
data = response.json()
except ValueError:
print("Respuesta JSON inválida")
return []
if "results" not in data or len(data["results"]) != len(coordinates):
print("Formato de respuesta inesperado")
return []
points = []
for result in data["results"]:
try:
easting, northing, _, _ = latlon_to_utm(
result["latitude"],
result["longitude"]
)
points.append(FreeCAD.Vector(round(easting),
round(northing),
round(result["elevation"])) * 1000)
except Exception as e:
print(f"Error procesando coordenadas: {str(e)}")
continue
return points
def getElevationFromOE(coordinates):
"""Obtiene elevaciones de Open-Elevation API y devuelve vectores FreeCAD en coordenadas UTM."""
import certifi
from requests.exceptions import RequestException
if len(coordinates) == 0:
return None
from requests import get
from lib.projection import latlon_to_utm
locations_str=""
total = len(coordinates) - 1
for i, point in enumerate(coordinates):
locations_str += '{:.6f},{:.6f}'.format(point[0], point[1])
if i != total:
locations_str += '|'
query = 'https://api.open-elevation.com/api/v1/lookup?locations=' + locations_str
points = []
try:
r = get(query, timeout=20, verify=certifi.where())
results = r.json()
for point in results["results"]:
easting, northing, _, _ = latlon_to_utm(point["latitude"], point["longitude"])
v = FreeCAD.Vector(round(easting, 0),
round(northing, 0),
round(point["elevation"], 0)) * 1000
points.append(v)
except RequestException as e:
for point in coordinates:
easting, northing, _, _ = latlon_to_utm(point[0], point[1])
points.append(FreeCAD.Vector(round(easting, 0),
round(northing, 0),
0) * 1000)
return points
def getSinglePointElevationFromBing(lat, lng):
import requests
from lib.projection import latlon_to_utm
source = "http://dev.virtualearth.net/REST/v1/Elevation/List?points="
source += str(lat) + "," + str(lng)
source += "&heights=sealevel"
source += "&key=AmsPZA-zRt2iuIdQgvXZIxme2gWcgLaz7igOUy7VPB8OKjjEd373eCnj1KFv2CqX"
response = requests.get(source)
ans = response.text
s = json.loads(ans)
print(s)
res = s['resourceSets'][0]['resources'][0]['elevations']
for elevation in res:
easting, northing, _, _ = latlon_to_utm(lat, lng)
v = FreeCAD.Vector(
round(easting * 1000, 0),
round(northing * 1000, 0),
round(elevation * 1000, 0))
return v
def getGridElevationFromBing(polygon, lat, lng, resolution = 1000):
import math
import requests
from lib.projection import latlon_to_utm, utm_to_latlon
_, _, zone_number, zone_letter = latlon_to_utm(lat, lng)
points = []
yy = polygon.Shape.BoundBox.YMax
while yy > polygon.Shape.BoundBox.YMin:
xx = polygon.Shape.BoundBox.XMin
while xx < polygon.Shape.BoundBox.XMax:
StepsXX = int(math.ceil((polygon.Shape.BoundBox.XMax - xx) / resolution))
if StepsXX > 1000:
StepsXX = 1000
xx1 = xx + 1000 * resolution
else:
xx1 = xx + StepsXX * resolution
point1 = utm_to_latlon(xx / 1000, yy / 1000, zone_number, zone_letter)
point2 = utm_to_latlon(xx1 / 1000, yy / 1000, zone_number, zone_letter)
source = "http://dev.virtualearth.net/REST/v1/Elevation/Polyline?points="
source += "{lat1},{lng1}".format(lat1=point1[0], lng1=point1[1])
source += ","
source += "{lat2},{lng2}".format(lat2=point2[0], lng2=point2[1])
source += "&heights=sealevel"
source += "&samples={steps}".format(steps=StepsXX)
source += "&key=AmsPZA-zRt2iuIdQgvXZIxme2gWcgLaz7igOUy7VPB8OKjjEd373eCnj1KFv2CqX"
response = requests.get(source)
ans = response.text
s = json.loads(ans)
res = s['resourceSets'][0]['resources'][0]['elevations']
i = 0
for elevation in res:
v = FreeCAD.Vector(xx + resolution * i, yy, round(elevation * 1000, 4))
points.append(v)
i += 1
xx = xx1 + resolution
yy -= resolution
return points
def getSinglePointElevation(lat, lon):
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
#print (source)
#response = request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
from geopy.distance import geodesic
for r in res:
reference = (0.0, 0.0)
v = FreeCAD.Vector(
round(geodesic(reference, (0.0, r['location']['lng'])).m, 2),
round(geodesic(reference, (r['location']['lat'], 0.0)).m, 2),
round(r['elevation'] * 1000, 2)
)
return v
def _getSinglePointElevation(lat, lon):
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
#print (source)
#response = request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
import pymap3d as pm
for r in res:
x, y, z = pm.geodetic2ecef(round(r['location']['lng'], 2),
round(r['location']['lat'], 2),
0)
v = FreeCAD.Vector(x,y,z)
return v
def getSinglePointElevation1(lat, lon):
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
#response = urllib.request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
for r in res:
c = tm.fromGeographic(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(c[0], 4),
round(c[1], 4),
round(r['elevation'] * 1000, 2)
)
return v
def getSinglePointElevationUtm(lat, lon):
import requests
from lib.projection import latlon_to_utm
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
print(source)
response = requests.get(source)
ans = response.text
s = json.loads(ans)
res = s['results']
print(res)
for r in res:
easting, northing, _, _ = latlon_to_utm(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(easting * 1000, 4),
round(northing * 1000, 4),
round(r['elevation'] * 1000, 2))
print(v)
return v
def getElevationUTM(polygon, lat, lng, resolution = 10000):
from lib.projection import latlon_to_utm, utm_to_latlon
_, _, zone_number, zone_letter = latlon_to_utm(lat, lng)
StepsXX = int((polygon.Shape.BoundBox.XMax - polygon.Shape.BoundBox.XMin) / (resolution*1000))
points = []
yy = polygon.Shape.BoundBox.YMax
while yy > polygon.Shape.BoundBox.YMin:
point1 = utm_to_latlon(polygon.Shape.BoundBox.XMin / 1000, yy / 1000, zone_number, zone_letter)
point2 = utm_to_latlon(polygon.Shape.BoundBox.XMax / 1000, yy / 1000, zone_number, zone_letter)
source = "https://maps.googleapis.com/maps/api/elevation/json?path="
source += "{a},{b}".format(a = point1[0], b = point1[1])
source += "|"
source += "{a},{b}".format(a = point2[0], b = point2[1])
source += "&samples={a}".format(a = StepsXX)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
import requests
response = requests.get(source)
ans = response.text
s = json.loads(ans)
res = s['results']
for r in res:
easting, northing, _, _ = latlon_to_utm(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(easting * 1000, 2),
round(northing * 1000, 2),
round(r['elevation'] * 1000, 2)
)
points.append(v)
yy -= (resolution*1000)
FreeCAD.activeDocument().recompute()
return points
def getElevation1(polygon,resolution=10):
StepsXX = int((polygon.Shape.BoundBox.XMax - polygon.Shape.BoundBox.XMin) / (resolution * 1000))
points = []
yy = polygon.Shape.BoundBox.YMax
while yy > polygon.Shape.BoundBox.YMin:
point1 = tm.toGeographic(polygon.Shape.BoundBox.XMin, yy)
point2 = tm.toGeographic(polygon.Shape.BoundBox.XMax, yy)
source = "https://maps.googleapis.com/maps/api/elevation/json?path="
source += "{a},{b}".format(a = point1[0], b = point1[1])
source += "|"
source += "{a},{b}".format(a = point2[0], b = point2[1])
source += "&samples={a}".format(a = StepsXX)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
try:
#response = urllib.request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
except:
continue
#points = []
for r in res:
c = tm.fromGeographic(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(c[0], 2),
round(c[1], 2),
round(r['elevation'] * 1000, 2)
)
points.append(v)
FreeCAD.activeDocument().recompute()
yy -= (resolution*1000)
return points
## download the heights from google:
def getElevation(lat, lon, b=50.35, le=11.17, size=40):
#https://maps.googleapis.com/maps/api/elevation/json?path=36.578581,-118.291994|36.23998,-116.83171&samples=3&key=YOUR_API_KEY
#https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034&key=YOUR_API_KEY
source = "https://maps.googleapis.com/maps/api/elevation/json?path="
source += str(b-size*0.001) + "," + str(le) + "|" + str(b+size*0.001) + "," + str(le)
source += "&samples=" + str(100)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
response = urllib.request.urlopen(source)
ans = response.read()
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
from geopy.distance import geodesic
points = []
for r in res:
reference = (0.0, 0.0)
v = FreeCAD.Vector(
round(geodesic(reference, (0.0, r['location']['lat'])).m, 2),
round(geodesic(reference, (r['location']['lng'], 0.0)).m, 2),
round(r['elevation'] * 1000, 2) - baseheight
)
points.append(v)
line = Draft.makeWire(points, closed=False, face=False, support=None)
line.ViewObject.Visibility = False
#FreeCAD.activeDocument().recompute()
FreeCADGui.updateGui()
return FreeCAD.activeDocument().ActiveObject
class _ImportPointsTaskPanel:
def __init__(self, obj = None):
self.obj = None
self.Boundary = None
self.select = 0
self.filename = ""
# form:
self.form1 = FreeCADGui.PySideUic.loadUi(os.path.dirname(__file__) + "/PVPlantImportGrid.ui")
self.form1.radio1.toggled.connect(lambda: self.mainToggle(self.form1.radio1))
self.form1.radio2.toggled.connect(lambda: self.mainToggle(self.form1.radio2))
self.form1.radio1.setChecked(True) # << --------------Poner al final para que no dispare antes de crear los componentes a los que va a llamar
#self.form.buttonAdd.clicked.connect(self.add)
self.form1.buttonDEM.clicked.connect(self.openFileDEM)
self.form2 = FreeCADGui.PySideUic.loadUi(os.path.dirname(__file__) + "/PVPlantCreateTerrainMesh.ui")
#self.form2.buttonAdd.clicked.connect(self.add)
self.form2.buttonBoundary.clicked.connect(self.addBoundary)
#self.form = [self.form1, self.form2]
self.form = self.form1
''' future:
def retranslateUi(self, dialog):
self.form1.setWindowTitle("Configuracion del Rack")
self.labelModule.setText(QtGui.QApplication.translate("PVPlant", "Modulo:", None))
self.labelModuleLength.setText(QtGui.QApplication.translate("PVPlant", "Longitud:", None))
self.labelModuleWidth.setText(QtGui.QApplication.translate("PVPlant", "Ancho:", None))
self.labelModuleHeight.setText(QtGui.QApplication.translate("PVPlant", "Alto:", None))
self.labelModuleFrame.setText(QtGui.QApplication.translate("PVPlant", "Ancho del marco:", None))
self.labelModuleColor.setText(QtGui.QApplication.translate("PVPlant", "Color del modulo:", None))
self.labelModules.setText(QtGui.QApplication.translate("Arch", "Colocacion de los Modulos", None))
self.labelModuleOrientation.setText(QtGui.QApplication.translate("Arch", "Orientacion del modulo:", None))
self.labelModuleGapX.setText(QtGui.QApplication.translate("Arch", "Separacion Horizontal (mm):", None))
self.labelModuleGapY.setText(QtGui.QApplication.translate("Arch", "Separacion Vertical (mm):", None))
self.labelModuleRows.setText(QtGui.QApplication.translate("Arch", "Filas de modulos:", None))
self.labelModuleCols.setText(QtGui.QApplication.translate("Arch", "Columnas de modulos:", None))
self.labelRack.setText(QtGui.QApplication.translate("Arch", "Configuracion de la estructura", None))
self.labelRackType.setText(QtGui.QApplication.translate("Arch", "Tipo de estructura:", None))
self.labelLevel.setText(QtGui.QApplication.translate("Arch", "Nivel:", None))
self.labelOffset.setText(QtGui.QApplication.translate("Arch", "Offset", None))
'''
def add(self):
sel = FreeCADGui.Selection.getSelection()
if len(sel) > 0:
self.obj = sel[0]
self.lineEdit1.setText(self.obj.Label)
def addBoundary(self):
sel = FreeCADGui.Selection.getSelection()
if len(sel) > 0:
self.Boundary = sel[0]
self.form2.editBoundary.setText(self.Boundary.Label)
def openFileDEM(self):
filters = "Esri ASC (*.asc);;CSV (*.csv);;All files (*.*)"
filename = QtGui.QFileDialog.getOpenFileName(None,
"Open DEM,",
"",
filters)
self.filename = filename[0]
self.form1.editDEM.setText(filename[0])
def mainToggle(self, radiobox):
if radiobox is self.form1.radio1:
self.select = 0
self.form1.gbLocalFile.setVisible(True)
elif radiobox is self.form1.radio2:
self.select = 1
self.form1.gbLocalFile.setVisible(True)
def accept(self):
from datetime import datetime
starttime = datetime.now()
site = PVPlantSite.get()
try:
PointGroups = FreeCAD.ActiveDocument.Point_Groups
except:
PointGroups = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", 'Point_Groups')
PointGroups.Label = "Point Groups"
PointGroup = FreeCAD.ActiveDocument.addObject('Points::Feature', "Point_Group")
PointGroup.Label = "Land_Grid_Points"
FreeCAD.ActiveDocument.Point_Groups.addObject(PointGroup)
PointObject = PointGroup.Points.copy()
if self.select == 0: # Google or bing or ...
#for item in self.obj:
#if self.groupbox.isChecked:break
resol = FreeCAD.Units.Quantity(self.valueResolution.text()).Value
Site = FreeCAD.ActiveDocument.Site
pts = getGridElevationFromBing(self.obj, Site.Latitude, Site.Longitude, resol)
PointObject.addPoints(pts)
PointGroup.Points = PointObject
else:
if self.filename == "":
return
import Utils.importDEM as openDEM
if self.select == 1: # DEM.
import numpy as np
root, extension = os.path.splitext(self.filename)
if extension.lower() == ".asc":
x, y, datavals, cellsize, nodata_value = openDEM.openEsri(self.filename)
if self.Boundary:
inc_x = self.Boundary.Shape.BoundBox.XLength * 0.05
inc_y = self.Boundary.Shape.BoundBox.YLength * 0.05
min_x = 0
max_x = 0
comp = (self.Boundary.Shape.BoundBox.XMin - inc_x) / 1000
for i in range(nx):
if x[i] > comp:
min_x = i - 1
break
comp = (self.Boundary.Shape.BoundBox.XMax + inc_x) / 1000
for i in range(min_x, nx):
if x[i] > comp:
max_x = i
break
min_y = 0
max_y = 0
comp = (self.Boundary.Shape.BoundBox.YMax + inc_y) / 1000
for i in range(ny):
if y[i] < comp:
max_y = i
break
comp = (self.Boundary.Shape.BoundBox.YMin - inc_y) / 1000
for i in range(max_y, ny):
if y[i] < comp:
min_y = i
break
x = x[min_x:max_x]
y = y[max_y:min_y]
datavals = datavals[max_y:min_y, min_x:max_x]
pts = []
if True: # faster but more memory 46s - 4,25 gb
x, y = np.meshgrid(x, y)
xx = x.flatten()
yy = y.flatten()
zz = datavals.flatten()
x[:] = 0
y[:] = 0
datavals[:] = 0
pts = []
for i in range(0, len(xx)):
pts.append(FreeCAD.Vector(xx[i], yy[i], zz[i]) * 1000)
xx[:] = 0
yy[:] = 0
zz[:] = 0
else: # 51s 3,2 gb
createmesh = True
if createmesh:
import Part, Draft
lines=[]
for j in range(len(y)):
edges = []
for i in range(0, len(x) - 1):
ed = Part.makeLine(FreeCAD.Vector(x[i], y[j], datavals[j][i]) * 1000,
FreeCAD.Vector(x[i + 1], y[j], datavals[j][i + 1]) * 1000)
edges.append(ed)
#bspline = Draft.makeBSpline(pts)
#bspline.ViewObject.hide()
line = Part.Wire(edges)
lines.append(line)
'''
for i in range(0, len(bsplines), 100):
p = Part.makeLoft(bsplines[i:i + 100], False, False, False)
Part.show(p)
'''
p = Part.makeLoft(lines, False, True, False)
p = Part.Solid(p)
Part.show(p)
else:
pts = []
for j in range(ny):
for i in range(nx):
pts.append(FreeCAD.Vector(x[i], y[j], datavals[j][i]) * 1000)
elif extension.lower() == ".csv" or extension.lower() == ".txt": # x, y, z from gps
pts = openDEM.interpolatePoints(openDEM.openCSV(self.filename))
PointObject.addPoints(pts)
PointGroup.Points = PointObject
FreeCAD.ActiveDocument.recompute()
FreeCADGui.Control.closeDialog()
print("tiempo: ", datetime.now() - starttime)
def reject(self):
FreeCADGui.Control.closeDialog()
## Comandos -----------------------------------------------------------------------------------------------------------
class CommandImportPoints:
def GetResources(self):
return {'Pixmap': str(os.path.join(DirIcons, "cloud.svg")),
'MenuText': QT_TRANSLATE_NOOP("PVPlant", "Importer Grid"),
'Accel': "B, U",
'ToolTip': QT_TRANSLATE_NOOP("PVPlant", "Creates a cloud of points.")}
def IsActive(self):
return not FreeCAD.ActiveDocument is None
def Activated(self):
self.TaskPanel = _ImportPointsTaskPanel()
FreeCADGui.Control.showDialog(self.TaskPanel)
if FreeCAD.GuiUp:
class CommandPointsGroup:
def GetCommands(self):
return tuple(['ImportPoints'
])
def GetResources(self):
return { 'MenuText': QT_TRANSLATE_NOOP("",'Cloud of Points'),
'ToolTip': QT_TRANSLATE_NOOP("",'Cloud of Points')
}
def IsActive(self):
return not FreeCAD.ActiveDocument is None
FreeCADGui.addCommand('ImportPoints', CommandImportPoints())
FreeCADGui.addCommand('PointsGroup', CommandPointsGroup())
View File
File diff suppressed because it is too large Load Diff
-945
View File
@@ -1,945 +0,0 @@
import math
import FreeCAD
import Part
import ArchComponent
from pivy import coin
import numpy as np
import DraftGeomUtils
if FreeCAD.GuiUp:
import FreeCADGui, os
from PySide import QtCore, QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
else:
# \cond
def translate(ctxt, txt):
return txt
def QT_TRANSLATE_NOOP(ctxt, txt):
return txt
# \endcond
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
import PVPlantResources
from PVPlantResources import DirIcons as DirIcons
voltype = ["Fill", "Cut"]
def makeEarthWorksVolume(vtype = 0):
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", voltype[vtype])
EarthWorksVolume(obj)
ViewProviderEarthWorksVolume(obj.ViewObject)
return obj
class EarthWorksVolume(ArchComponent.Component):
def __init__(self, obj):
# Definición de Variables:
ArchComponent.Component.__init__(self, obj)
self.obj = obj
self.setProperties(obj)
def setProperties(self, obj):
# Definicion de Propiedades:
pl = obj.PropertiesList
if not ("VolumeType" in pl):
obj.addProperty("App::PropertyEnumeration",
"VolumeType",
"Volume",
"Connection").VolumeType = voltype
if not ("SurfaceSlope" in pl):
obj.addProperty("App::PropertyPercent",
"SurfaceSlope",
"Volume",
"Connection").SurfaceSlope = 2
if not ("VolumeMesh" in pl):
obj.addProperty("Mesh::PropertyMeshKernel",
"VolumeMesh",
"Volume",
"Volume")
obj.setEditorMode("VolumeMesh", 2)
if not ("Volume" in pl):
obj.addProperty("App::PropertyVolume",
"Volume",
"Volume",
"Volume")
obj.setEditorMode("Volume", 1)
obj.Proxy = self
obj.IfcType = "Civil Element"
obj.setEditorMode("IfcType", 1)
obj.Proxy = self
def onDocumentRestored(self, obj):
ArchComponent.Component.onDocumentRestored(self, obj)
self.setProperties(obj)
def onChange(self, obj, prop):
if prop == "VolumeMesh":
if obj.VolumeMesh:
obj.VolumeMesh = obj.VolumeMesh.Volume
def execute(self, obj):
''' '''
pass
class ViewProviderEarthWorksVolume:
"A View Provider for the Pipe object"
def __init__(self, vobj):
''' Set view properties. '''
pl = vobj.PropertiesList
(r, g, b) = (1.0, 0.0, 0.0) if vobj.Object.VolumeType == "Cut" else (0.0, 0.0, 1.0)
# Triangulation properties.
if not "Transparency" in pl:
vobj.addProperty("App::PropertyIntegerConstraint",
"Transparency", "Surface Style",
"Set triangle face transparency")
vobj.Transparency = (50, 0, 100, 1)
if not "ShapeColor" in pl:
vobj.addProperty("App::PropertyColor",
"ShapeColor",
"Surface Style",
"Set triangle face color")
vobj.ShapeColor = (r, g, b, vobj.Transparency / 100)
if not "ShapeMaterial" in pl:
vobj.addProperty("App::PropertyMaterial",
"ShapeMaterial", "Surface Style",
"Triangle face material")
vobj.ShapeMaterial = FreeCAD.Material()
if not "LineTransparency" in pl:
vobj.addProperty("App::PropertyIntegerConstraint",
"LineTransparency", "Surface Style",
"Set triangle edge transparency")
vobj.LineTransparency = (50, 0, 100, 1)
if not "LineColor" in pl:
vobj.addProperty("App::PropertyColor",
"LineColor", "Surface Style",
"Set triangle face color")
vobj.LineColor = (0.5, 0.5, 0.5, vobj.LineTransparency / 100)
'''vobj.addProperty(
"App::PropertyMaterial", "LineMaterial", "Surface Style",
"Triangle face material").LineMaterial = FreeCAD.Material()
vobj.addProperty(
"App::PropertyFloatConstraint", "LineWidth", "Surface Style",
"Set triangle edge line width").LineWidth = (0.0, 1.0, 20.0, 1.0)
# Boundary properties.
vobj.addProperty(
"App::PropertyColor", "BoundaryColor", "Boundary Style",
"Set boundary contour color").BoundaryColor = (0.0, 0.75, 1.0, 0.0)
vobj.addProperty(
"App::PropertyFloatConstraint", "BoundaryWidth", "Boundary Style",
"Set boundary contour line width").BoundaryWidth = (3.0, 1.0, 20.0, 1.0)
vobj.addProperty(
"App::PropertyEnumeration", "BoundaryPattern", "Boundary Style",
"Set a line pattern for boundary").BoundaryPattern = [*line_patterns]
vobj.addProperty(
"App::PropertyIntegerConstraint", "PatternScale", "Boundary Style",
"Scale the line pattern").PatternScale = (3, 1, 20, 1)
# Contour properties.
vobj.addProperty(
"App::PropertyColor", "MajorColor", "Contour Style",
"Set major contour color").MajorColor = (1.0, 0.0, 0.0, 0.0)
vobj.addProperty(
"App::PropertyFloatConstraint", "MajorWidth", "Contour Style",
"Set major contour line width").MajorWidth = (4.0, 1.0, 20.0, 1.0)
vobj.addProperty(
"App::PropertyColor", "MinorColor", "Contour Style",
"Set minor contour color").MinorColor = (1.0, 1.0, 0.0, 0.0)
vobj.addProperty(
"App::PropertyFloatConstraint", "MinorWidth", "Contour Style",
"Set major contour line width").MinorWidth = (2.0, 1.0, 20.0, 1.0)
'''
vobj.Proxy = self
vobj.ShapeMaterial.DiffuseColor = vobj.ShapeColor
def onChanged(self, vobj, prop):
'''
Update Object visuals when a view property changed.
'''
if prop == "ShapeColor" or prop == "Transparency":
if hasattr(vobj, "ShapeColor") and hasattr(vobj, "Transparency"):
color = vobj.getPropertyByName("ShapeColor")
transparency = vobj.getPropertyByName("Transparency")
color = (color[0], color[1], color[2], transparency / 100)
vobj.ShapeMaterial.DiffuseColor = color
if prop == "ShapeMaterial":
if hasattr(vobj, "ShapeMaterial"):
material = vobj.getPropertyByName("ShapeMaterial")
self.face_material.diffuseColor.setValue(material.DiffuseColor[:3])
self.face_material.transparency = material.DiffuseColor[3]
if prop == "LineColor" or prop == "LineTransparency":
if hasattr(vobj, "LineColor") and hasattr(vobj, "LineTransparency"):
color = vobj.getPropertyByName("LineColor")
transparency = vobj.getPropertyByName("LineTransparency")
color = (color[0], color[1], color[2], transparency / 100)
vobj.LineMaterial.DiffuseColor = color
if prop == "LineMaterial":
material = vobj.getPropertyByName(prop)
self.edge_material.diffuseColor.setValue(material.DiffuseColor[:3])
self.edge_material.transparency = material.DiffuseColor[3]
if prop == "LineWidth":
width = vobj.getPropertyByName(prop)
self.edge_style.lineWidth = width
if prop == "BoundaryColor":
color = vobj.getPropertyByName(prop)
self.boundary_color.rgb = color[:3]
if prop == "BoundaryWidth":
width = vobj.getPropertyByName(prop)
self.boundary_style.lineWidth = width
if prop == "BoundaryPattern":
if hasattr(vobj, "BoundaryPattern"):
pattern = vobj.getPropertyByName(prop)
self.boundary_style.linePattern = line_patterns[pattern]
if prop == "PatternScale":
if hasattr(vobj, "PatternScale"):
scale = vobj.getPropertyByName(prop)
self.boundary_style.linePatternScaleFactor = scale
if prop == "MajorColor":
color = vobj.getPropertyByName(prop)
self.major_color.rgb = color[:3]
if prop == "MajorWidth":
width = vobj.getPropertyByName(prop)
self.major_style.lineWidth = width
if prop == "MinorColor":
color = vobj.getPropertyByName(prop)
self.minor_color.rgb = color[:3]
if prop == "MinorWidth":
width = vobj.getPropertyByName(prop)
self.minor_style.lineWidth = width
def attach(self, vobj):
'''
Create Object visuals in 3D view.
'''
# GeoCoords Node.
self.geo_coords = coin.SoGeoCoordinate()
# Surface features.
self.triangles = coin.SoIndexedFaceSet()
self.face_material = coin.SoMaterial()
self.edge_material = coin.SoMaterial()
self.edge_color = coin.SoBaseColor()
self.edge_style = coin.SoDrawStyle()
self.edge_style.style = coin.SoDrawStyle.LINES
shape_hints = coin.SoShapeHints()
shape_hints.vertex_ordering = coin.SoShapeHints.COUNTERCLOCKWISE
mat_binding = coin.SoMaterialBinding()
mat_binding.value = coin.SoMaterialBinding.PER_FACE
offset = coin.SoPolygonOffset()
offset.styles = coin.SoPolygonOffset.LINES
offset.factor = -2.0
# Boundary features.
self.boundary_color = coin.SoBaseColor()
self.boundary_coords = coin.SoGeoCoordinate()
self.boundary_lines = coin.SoLineSet()
self.boundary_style = coin.SoDrawStyle()
self.boundary_style.style = coin.SoDrawStyle.LINES
# Boundary root.
boundaries = coin.SoType.fromName('SoFCSelection').createInstance()
boundaries.style = 'EMISSIVE_DIFFUSE'
boundaries.addChild(self.boundary_color)
boundaries.addChild(self.boundary_style)
boundaries.addChild(self.boundary_coords)
boundaries.addChild(self.boundary_lines)
# Major Contour features.
self.major_color = coin.SoBaseColor()
self.major_coords = coin.SoGeoCoordinate()
self.major_lines = coin.SoLineSet()
self.major_style = coin.SoDrawStyle()
self.major_style.style = coin.SoDrawStyle.LINES
# Major Contour root.
major_contours = coin.SoSeparator()
major_contours.addChild(self.major_color)
major_contours.addChild(self.major_style)
major_contours.addChild(self.major_coords)
major_contours.addChild(self.major_lines)
# Minor Contour features.
self.minor_color = coin.SoBaseColor()
self.minor_coords = coin.SoGeoCoordinate()
self.minor_lines = coin.SoLineSet()
self.minor_style = coin.SoDrawStyle()
self.minor_style.style = coin.SoDrawStyle.LINES
# Minor Contour root.
minor_contours = coin.SoSeparator()
minor_contours.addChild(self.minor_color)
minor_contours.addChild(self.minor_style)
minor_contours.addChild(self.minor_coords)
minor_contours.addChild(self.minor_lines)
# Highlight for selection.
highlight = coin.SoType.fromName('SoFCSelection').createInstance()
highlight.style = 'EMISSIVE_DIFFUSE'
highlight.addChild(shape_hints)
highlight.addChild(mat_binding)
highlight.addChild(self.geo_coords)
highlight.addChild(self.triangles)
highlight.addChild(boundaries)
# Face root.
face = coin.SoSeparator()
face.addChild(self.face_material)
face.addChild(highlight)
# Edge root.
edge = coin.SoSeparator()
edge.addChild(self.edge_material)
edge.addChild(self.edge_style)
edge.addChild(highlight)
# Surface root.
surface_root = coin.SoSeparator()
surface_root.addChild(face)
surface_root.addChild(offset)
surface_root.addChild(edge)
surface_root.addChild(major_contours)
surface_root.addChild(minor_contours)
vobj.addDisplayMode(surface_root, "Surface")
# Boundary root.
boundary_root = coin.SoSeparator()
boundary_root.addChild(boundaries)
vobj.addDisplayMode(boundary_root, "Boundary")
# Elevation/Shaded root.
shaded_root = coin.SoSeparator()
shaded_root.addChild(face)
vobj.addDisplayMode(shaded_root, "Elevation")
vobj.addDisplayMode(shaded_root, "Slope")
vobj.addDisplayMode(shaded_root, "Shaded")
# Flat Lines root.
flatlines_root = coin.SoSeparator()
flatlines_root.addChild(face)
flatlines_root.addChild(offset)
flatlines_root.addChild(edge)
vobj.addDisplayMode(flatlines_root, "Flat Lines")
# Wireframe root.
wireframe_root = coin.SoSeparator()
wireframe_root.addChild(edge)
wireframe_root.addChild(major_contours)
wireframe_root.addChild(minor_contours)
vobj.addDisplayMode(wireframe_root, "Wireframe")
# Take features from properties.
self.onChanged(vobj, "ShapeColor")
self.onChanged(vobj, "LineColor")
self.onChanged(vobj, "LineWidth")
'''self.onChanged(vobj, "BoundaryColor")
self.onChanged(vobj, "BoundaryWidth")
self.onChanged(vobj, "BoundaryPattern")
self.onChanged(vobj, "PatternScale")
self.onChanged(vobj, "MajorColor")
self.onChanged(vobj, "MajorWidth")
self.onChanged(vobj, "MinorColor")
self.onChanged(vobj, "MinorWidth")'''
def updateData(self, obj, prop):
'''
Update Object visuals when a data property changed.
'''
# Set System.
geo_system = ["UTM", FreeCAD.ActiveDocument.Site.UtmZone, "FLAT"]
self.geo_coords.geoSystem.setValues(geo_system)
self.boundary_coords.geoSystem.setValues(geo_system)
self.major_coords.geoSystem.setValues(geo_system)
self.minor_coords.geoSystem.setValues(geo_system)
if prop == "VolumeMesh":
mesh = obj.VolumeMesh
copy_mesh = mesh.copy()
#copy_mesh.Placement.move(origin.Origin)
triangles = []
for i in copy_mesh.Topology[1]:
triangles.extend(list(i))
triangles.append(-1)
self.geo_coords.point.values = copy_mesh.Topology[0]
self.triangles.coordIndex.values = triangles
del copy_mesh
'''if prop == "ContourShapes":
contour_shape = obj.getPropertyByName(prop)
if contour_shape.SubShapes:
major_shape = contour_shape.SubShapes[0]
points, vertices = self.wire_view(major_shape, origin.Origin)
self.major_coords.point.values = points
self.major_lines.numVertices.values = vertices
minor_shape = contour_shape.SubShapes[1]
points, vertices = self.wire_view(minor_shape, origin.Origin)
self.minor_coords.point.values = points
self.minor_lines.numVertices.values = vertices
if prop == "BoundaryShapes":
boundary_shape = obj.getPropertyByName(prop)
points, vertices = self.wire_view(boundary_shape, origin.Origin, True)
self.boundary_coords.point.values = points
self.boundary_lines.numVertices.values = vertices
if prop == "AnalysisType" or prop == "Ranges":
analysis_type = obj.getPropertyByName("AnalysisType")
ranges = obj.getPropertyByName("Ranges")
if analysis_type == "Default":
if hasattr(obj.ViewObject, "ShapeMaterial"):
material = obj.ViewObject.ShapeMaterial
self.face_material.diffuseColor = material.DiffuseColor[:3]
if analysis_type == "Elevation":
colorlist = self.elevation_analysis(obj.Mesh, ranges)
self.face_material.diffuseColor.setValues(0, len(colorlist), colorlist)
elif analysis_type == "Slope":
colorlist = self.slope_analysis(obj.Mesh, ranges)
self.face_material.diffuseColor.setValues(0, len(colorlist), colorlist)
'''
def getIcon(self):
""" Return the path to the appropriate icon. """
return str(os.path.join(DirIcons, "solar-fixed.svg"))
def getDisplayModes(self, vobj):
'''
Return a list of display modes.
'''
modes = ["Surface", "Boundary", "Flat Lines", "Shaded", "Wireframe"]
return modes
def getDefaultDisplayMode(self):
'''
Return the name of the default display mode.
'''
return "Surface"
def setDisplayMode(self, mode):
'''
Map the display mode defined in attach with
those defined in getDisplayModes.
'''
return mode
def __getstate__(self):
"""
Save variables to file.
"""
return None
def __setstate__(self, state):
"""
Get variables from file.
"""
return None
class EarthWorksTaskPanel:
def __init__(self):
self.To = None
# self.form:
self.form = FreeCADGui.PySideUic.loadUi(os.path.join(PVPlantResources.__dir__, "PVPlantEarthworks.ui"))
self.form.setWindowIcon(QtGui.QIcon(os.path.join(PVPlantResources.DirIcons, "convert.svg")))
def accept(self):
from datetime import datetime
starttime = datetime.now()
import MeshPart as mp
land = FreeCAD.ActiveDocument.Terrain.Mesh.copy()
frames = []
for obj in FreeCADGui.Selection.getSelection():
if hasattr(obj, "Proxy"):
if obj.Proxy.Type == "Tracker":
if not (obj in frames):
frames.append(obj)
elif obj.Proxy.Type == "FrameArea":
for fr in obj.Frames:
if not (fr in frames):
frames.append(fr)
if len(frames) == 0:
return False
FreeCAD.ActiveDocument.openTransaction("Calcular movimiento de tierras")
def calculateEarthWorks(line, extreme=False):
pts = []
pts1 = []
line1 = line.copy()
angles = line.Placement.Rotation.toEulerAngles("XYZ")
line1.Placement.Rotation.setEulerAngles("XYZ", 0, 0, angles[2])
line1.Placement.Base.z = 0
pro = mp.projectShapeOnMesh(line1, land, FreeCAD.Vector(0, 0, 1))
flat = []
for points in pro:
flat.extend(points)
pro = Part.makePolygon(flat)
points = pro.discretize(Distance=500)
for point in points:
ver = Part.Vertex(point)
dist = ver.distToShape(line)
linepoint = dist[1][0][1]
if not extreme:
if self.form.groupTolerances.isChecked():
if linepoint.z > point.z:
if linepoint.sub(point).Length > self.form.editToleranceCut.value():
pts.append(linepoint)
elif linepoint.z < point.z:
if linepoint.sub(point).Length > self.form.editToleranceFill.value():
pts1.append(linepoint)
else:
if linepoint.z > point.z:
pts.append(linepoint)
elif linepoint.z < point.z:
pts1.append(linepoint)
#pts.append(linepoint)
else:
if linepoint.z > point.z:
if linepoint.sub(point).Length > 200:
pts.append(linepoint)
return pts, pts1
tools = [[],[]]
ver = 2
if ver == 0:
frames = sorted(frames, key=lambda x: (x.Placement.Base.x, x.Placement.Base.y))
for frame in frames:
length = frame.Setup.Length.Value / 2
p1 = FreeCAD.Vector(-length, 0, 0)
p2 = FreeCAD.Vector(length, 0, 0)
line = Part.LineSegment(p1, p2).toShape()
line.Placement = frame.Placement.copy()
line.Placement.Base.x = frame.Shape.BoundBox.XMin
step = (frame.Shape.BoundBox.XMax - frame.Shape.BoundBox.XMin) / 2
for n in range(3):
ret = calculateEarthWorks(line, n % 2)
tools[0].extend(ret[0])
tools[1].extend(ret[1])
line.Placement.Base.x += step
elif ver == 1:
from PVPlantPlacement import getCols
columns = getCols(frames)
for groups in columns:
for group in groups:
first = group[0]
last = group[-1]
for frame in group:
length = frame.Setup.Length.Value / 2
p1 = FreeCAD.Vector(-(length + (self.form.editOffset.value() if frame == first else -1000)),
0, 0)
p2 = FreeCAD.Vector(length + (self.form.editOffset.value() if frame == last else -1000),
0, 0)
line = Part.LineSegment(p1, p2).toShape()
line.Placement = frame.Placement.copy()
line.Placement.Base.x = frame.Shape.BoundBox.XMin
step = (frame.Shape.BoundBox.XMax - frame.Shape.BoundBox.XMin) / 2
for n in range(3):
ret = calculateEarthWorks(line, n % 2 == 1)
tools[0].extend(ret[0])
tools[1].extend(ret[1])
line.Placement.Base.x += step
elif ver == 2:
print("versión 2")
import PVPlantPlacement
rows, columns = PVPlantPlacement.getRows(frames)
if (rows is None) or (columns is None):
print("Nada que procesar")
return False
tools = []
lofts = []
for group in rows:
lines = []
cont = 0
while cont < len(group):
aw = 0
if cont > 0:
p0 = FreeCAD.Vector(group[cont - 1].Placement.Base)
p1 = FreeCAD.Vector(group[cont].Placement.Base)
aw = getAngle(p0, p1)
ae = 0
if cont < (len(group) - 1):
p1 = FreeCAD.Vector(group[cont].Placement.Base)
p2 = FreeCAD.Vector(group[cont + 1].Placement.Base)
ae = getAngle(p1, p2)
lng = int(group[cont].Setup.Length / 2)
wdt = int(group[cont].Setup.Width / 2)
line = Part.LineSegment(FreeCAD.Vector(-lng, 0, 0),
FreeCAD.Vector(lng, 0, 0)).toShape()
line = Part.LineSegment(FreeCAD.Vector(group[cont].Setup.Shape.SubShapes[1].SubShapes[0].SubShapes[0].Placement.Base.x, 0, 0),
FreeCAD.Vector(group[cont].Setup.Shape.SubShapes[1].SubShapes[0].SubShapes[-1].Placement.Base.x, 0, 0)).toShape()
anf = (aw + ae) / 2
if anf > FreeCAD.ActiveDocument.MaximumWestEastSlope.Value:
anf = FreeCAD.ActiveDocument.MaximumWestEastSlope.Value
zz = wdt * math.sin(math.radians(anf))
li = line.copy()
li.Placement = group[cont].Placement
li.Placement.Rotation = group[cont].Placement.Rotation
li.Placement.Base.x -= wdt #+ (3000 if cont == 0 else 0))
li.Placement.Base.z -= zz
lines.append(li)
ld = line.copy()
ld.Placement = group[cont].Placement
ld.Placement.Rotation = group[cont].Placement.Rotation
ld.Placement.Base.x += wdt #+ (3000 if cont == len(group) - 1 else 0))
ld.Placement.Base.z += zz
lines.append(ld)
tools.append([group[cont], li, ld])
cont += 1
loft = Part.makeLoft(lines, False, True, False)
lofts.append(loft)
for group in rows:
lines = []
for frame in group:
col, idx = searchFrameInColumns(frame, columns)
tool = searchTool(frame, tools)
if idx == 0:
''' '''
if idx == (len(col) - 1):
''' '''
if (idx + 1) < len(col):
frame1 = col[idx + 1]
tool1 = searchTool(frame1, tools)
line = Part.LineSegment(tool[1].Vertexes[1].Point, tool1[1].Vertexes[0].Point).toShape()
lines.append(line)
line = Part.LineSegment(tool[2].Vertexes[1].Point, tool1[2].Vertexes[0].Point).toShape()
lines.append(line)
if len(lines) > 0:
loft = Part.makeLoft(lines, False, True, False)
lofts.append(loft)
faces = []
for loft in lofts:
faces.extend(loft.Faces)
sh = Part.makeShell(faces)
import Utils.PVPlantUtils as utils
import Mesh
pro = utils.getProjected(sh)
pro = utils.simplifyWire(pro)
pts = [ver.Point for ver in pro.Vertexes]
land.trim(pts, 1)
tmp = []
shp = Part.Shape()
for face in sh.Faces:
wire = face.Wires[0].copy()
pl = wire.Placement.Base
wire.Placement.Base = wire.Placement.Base - pl
if DraftGeomUtils.isPlanar(wire):
# Caso simple
wire = wire.makeOffset2D(10000, 0, False, False, True)
wire.Placement.Base.z = wire.Placement.Base.z - 10000
top = wire.makeOffset2D(1, 0, False, False, True)
loft = Part.makeLoft([top, wire], True, True, False)
tmp.append(loft)
shp = shp.fuse(loft)
else:
# Caso complejo:
vertices = face.Vertexes
# Dividir rectángulo en 2 triángulos
triangles = [
[vertices[0], vertices[1], vertices[2]],
[vertices[2], vertices[3], vertices[0]]
]
for tri in triangles:
# Crear wire triangular
wire = Part.makePolygon([v.Point for v in tri] + [tri[0].Point])
wire = wire.makeOffset2D(10000, 0, False, False, True)
wire.Placement.Base.z = wire.Placement.Base.z - 10000
top = wire.makeOffset2D(1, 0, False, False, True)
loft = Part.makeLoft([top, wire], True, True, False)
tmp.append(loft)
shp = shp.fuse(loft)
final_tool = Part.makeCompound(tmp)
Part.show(final_tool, "tool")
Part.show(shp)
FreeCAD.ActiveDocument.commitTransaction()
self.closeForm()
return True
import MeshTools.Triangulation as TriangulateMesh
import MeshTools.MeshGetBoundary as mgb
import Mesh
for ind, points in enumerate(tools):
mesh = TriangulateMesh.Triangulate(points, MaxlengthLE=3000, MaxAngleLE=math.radians(100))
if mesh:
for mesh in mesh.getSeparateComponents():
boundary = mgb.get_boundary(mesh)
Part.show(boundary)
'''if self.form.editOffset.value() != 0:
import Utils.PVPlantUtils as utils
pro = utils.getProjected(boundary)
pro = pro.makeOffset2D(self.form.editOffset.value(), 0, False, False, True)
# TODO: paso intermedio de restar las areas prohibidas
pro = mp.projectShapeOnMesh(pro, land, FreeCAD.Vector(0, 0, 1))
cnt = 0
for lp in pro:
cnt += len(lp)
# points.extend(boundary.Wires[0].discretize(Number=cnt))
points = boundary.Wires[0].discretize(Distance=cnt)
for lp in pro:
points.extend(lp)
mesh1 = TriangulateMesh.Triangulate(points, MaxlengthLE=5000) # , MaxAngleLE=math.pi / 1.334)
import Mesh
Mesh.show(mesh1)
boundary = Part.makeCompound([])
for section in pro:
if len(section) > 0:
try:
boundary.add(Part.makePolygon(section))
except:
pass
Part.show(boundary)'''
#mesh.smooth("Laplace", 3)
#Mesh.show(mesh)
#Part.show(boundary)
vol = makeEarthWorksVolume(ind)
vol.VolumeMesh = mesh.copy()
if ind == 0:
''' put inside fills group '''
else:
''' put inside fills group '''
FreeCAD.ActiveDocument.commitTransaction()
self.closeForm()
return True
def reject(self):
self.closeForm()
return True
def closeForm(self):
FreeCADGui.Control.closeDialog()
def getAngle(vec1, vec2):
dX = vec2.x - vec1.x
dZ = vec2.z - vec1.z
return math.degrees(math.atan2(float(dZ), float(dX)))
def searchFrameInColumns(obj, columns):
for colidx, col in enumerate(columns):
for group in col:
if obj in group:
return group, group.index(obj) #groupidx
def searchTool(obj, tools):
for tool in tools:
if obj in tool:
return tool
'''class _CommandCalculateEarthworks:
def GetResources(self):
return {'Pixmap': str(os.path.join(PVPlantResources.DirIcons, "pico.svg")),
'Accel': "C, E",
'MenuText': QT_TRANSLATE_NOOP("Placement", "Movimiento de tierras"),
'ToolTip': QT_TRANSLATE_NOOP("Placement", "Calcular el movimiento de tierras")}
def Activated(self):
TaskPanel = _EarthWorksTaskPanel()
FreeCADGui.Control.showDialog(TaskPanel)
def IsActive(self):
active = not (FreeCAD.ActiveDocument is None)
if not (FreeCAD.ActiveDocument.getObject("Terrain") is None):
active = active and not (FreeCAD.ActiveDocument.getObject("Terrain").Mesh is None)
return active
if FreeCAD.GuiUp:
FreeCADGui.addCommand('PVPlantEarthworks', _CommandCalculateEarthworks())'''
def accept():
import MeshPart as mp
land = FreeCAD.ActiveDocument.Terrain.Mesh
frames = []
for obj in FreeCADGui.Selection.getSelection():
if hasattr(obj, "Proxy"):
if obj.Proxy.Type == "Tracker":
if not (obj in frames):
frames.append(obj)
elif obj.Proxy.Type == "FrameArea":
for fr in obj.Frames:
if not (fr in frames):
frames.append(fr)
if len(frames) == 0:
return False
FreeCAD.ActiveDocument.openTransaction("Calcular movimiento de tierras")
import PVPlantPlacement
rows, columns = PVPlantPlacement.getRows(frames)
if (rows is None) or (columns is None):
print("Nada que procesar")
return False
tools = []
for group in rows:
lines = []
cont = 0
while cont < len(group):
aw = 0
if cont > 0:
p0 = FreeCAD.Vector(group[cont - 1].Placement.Base)
p1 = FreeCAD.Vector(group[cont].Placement.Base)
aw = getAngle(p0, p1)
ae = 0
if cont < (len(group) - 1):
p1 = FreeCAD.Vector(group[cont].Placement.Base)
p2 = FreeCAD.Vector(group[cont + 1].Placement.Base)
ae = getAngle(p1, p2)
lng = int(group[cont].Setup.Length / 2)
wdt = int(group[cont].Setup.Width / 2)
line = Part.LineSegment(FreeCAD.Vector(-lng, 0, 0),
FreeCAD.Vector(lng, 0, 0)).toShape()
line = Part.LineSegment(FreeCAD.Vector(group[cont].Setup.Shape.SubShapes[1].SubShapes[0].SubShapes[0].Placement.Base.x, 0, 0),
FreeCAD.Vector(group[cont].Setup.Shape.SubShapes[1].SubShapes[0].SubShapes[-1].Placement.Base.x, 0, 0)).toShape()
anf = (aw + ae) / 2
if anf > FreeCAD.ActiveDocument.MaximumWestEastSlope.Value:
anf = FreeCAD.ActiveDocument.MaximumWestEastSlope.Value
zz = wdt * math.sin(math.radians(anf))
li = line.copy()
li.Placement = group[cont].Placement
li.Placement.Rotation = group[cont].Placement.Rotation
li.Placement.Base.x -= wdt #+ (3000 if cont == 0 else 0))
li.Placement.Base.z -= zz
lines.append(li)
ld = line.copy()
ld.Placement = group[cont].Placement
ld.Placement.Rotation = group[cont].Placement.Rotation
ld.Placement.Base.x += wdt #+ (3000 if cont == len(group) - 1 else 0))
ld.Placement.Base.z += zz
lines.append(ld)
tools.append([group[cont], li, ld])
cont += 1
loft = Part.makeLoft(lines, False, True, False)
import MeshPart as mp
msh = mp.meshFromShape(Shape=loft) #, MaxLength=1)
#msh = msh.smooth("Laplace", 3)
import Mesh
Mesh.show(msh)
'''intersec = land.section(msh, MinDist=0.01)
import Draft
for sec in intersec:
Draft.makeWire(sec)'''
for group in rows:
lines = []
for frame in group:
col, idx = searchFrameInColumns(frame, columns)
tool = searchTool(frame, tools)
if idx == 0:
''' '''
if idx == (len(col) - 1):
''' '''
if (idx + 1) < len(col):
frame1 = col[idx + 1]
tool1 = searchTool(frame1, tools)
line = Part.LineSegment(tool[1].Vertexes[1].Point, tool1[1].Vertexes[0].Point).toShape()
Part.show(line)
lines.append(line)
line = Part.LineSegment(tool[2].Vertexes[1].Point, tool1[2].Vertexes[0].Point).toShape()
Part.show(line)
lines.append(line)
if len(lines) > 0:
loft = Part.makeLoft(lines, False, True, False)
import MeshPart as mp
msh = mp.meshFromShape(Shape=loft) # , MaxLength=1)
#msh = msh.smooth("Laplace", 3)
import Mesh
Mesh.show(msh)
intersec = land.section(msh, MinDist=0.01)
import Draft
for sec in intersec:
Draft.makeWire(sec)
FreeCAD.ActiveDocument.commitTransaction()
self.closeForm()
return True
+580 -5
View File
@@ -20,10 +20,585 @@
# * *
# ***********************************************************************
"""
PVPlantGeoreferencing - Wrapper de compatibilidad.
import FreeCAD
import utm
Código movido a PVPlant/core/georef.py.
"""
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtCore, QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
from PVPlant.core.georef import MapWindow, CommandPVPlantGeoreferencing
import os
else:
# \cond
def translate(ctxt,txt):
return txt
def QT_TRANSLATE_NOOP(ctxt,txt):
return txt
# \endcond
import PVPlantResources
from PVPlantResources import DirIcons as DirIcons
from PVPlantResources import DirResources as DirResources
class MapWindow(QtGui.QWidget):
def __init__(self, WinTitle="MapWindow"):
super(MapWindow, self).__init__()
self.raise_()
self.lat = None
self.lon = None
self.minLat = None
self.maxLat = None
self.minLon = None
self.maxLon = None
self.zoom = None
self.WinTitle = WinTitle
self.georeference_coordinates = {'lat': None, 'lon': None}
self.setupUi()
def setupUi(self):
# Intentar cargar QtWebEngine (no siempre disponible, ej: FreeCAD flatpak)
QWebEngineView, QWebChannel = self._load_webengine()
self._webengine_available = QWebEngineView is not None
self.ui = FreeCADGui.PySideUic.loadUi(PVPlantResources.__dir__ + "/PVPlantGeoreferencing.ui", self)
self.resize(1200, 800)
self.setWindowTitle(self.WinTitle)
self.setWindowIcon(QtGui.QIcon(os.path.join(DirIcons, "Location.svg")))
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.layout = QtGui.QHBoxLayout(self)
self.layout.setContentsMargins(4, 4, 4, 4)
LeftWidget = QtGui.QWidget(self)
LeftLayout = QtGui.QVBoxLayout(LeftWidget)
LeftWidget.setLayout(LeftLayout)
LeftLayout.setContentsMargins(0, 0, 0, 0)
RightWidget = QtGui.QWidget(self)
RightWidget.setFixedWidth(350)
RightLayout = QtGui.QVBoxLayout(RightWidget)
RightWidget.setLayout(RightLayout)
RightLayout.setContentsMargins(0, 0, 0, 0)
self.layout.addWidget(LeftWidget)
self.layout.addWidget(RightWidget)
# Left Widgets:
if self._webengine_available:
# -- Search Bar:
self.valueSearch = QtGui.QLineEdit(self)
self.valueSearch.setPlaceholderText("Search")
self.valueSearch.returnPressed.connect(self.onSearch)
searchbutton = QtGui.QPushButton('Search')
searchbutton.setFixedWidth(80)
searchbutton.clicked.connect(self.onSearch)
SearchBarLayout = QtGui.QHBoxLayout(self)
SearchBarLayout.addWidget(self.valueSearch)
SearchBarLayout.addWidget(searchbutton)
LeftLayout.addLayout(SearchBarLayout)
# -- Web browser:
self.view = QWebEngineView()
self.channel = QWebChannel(self.view.page())
self.view.page().setWebChannel(self.channel)
self.channel.registerObject("MyApp", self)
file = os.path.join(DirResources, "webs", "main.html")
self.view.page().loadFinished.connect(self.onLoadFinished)
self.view.page().load(QtCore.QUrl.fromLocalFile(file))
LeftLayout.addWidget(self.view)
else:
# -- Modo manual: entrada de coordenadas sin mapa web
self.valueSearch = QtGui.QLineEdit(self)
self.valueSearch.setPlaceholderText("Latitud, Longitud (ej: 40.4168, -3.7038)")
self.valueSearch.returnPressed.connect(self.onManualCoords)
searchbutton = QtGui.QPushButton('Ir')
searchbutton.setFixedWidth(80)
searchbutton.clicked.connect(self.onManualCoords)
SearchBarLayout = QtGui.QHBoxLayout(self)
SearchBarLayout.addWidget(self.valueSearch)
SearchBarLayout.addWidget(searchbutton)
LeftLayout.addLayout(SearchBarLayout)
info = QtGui.QLabel("Mapa web no disponible. Introduce coordenadas manualmente.")
info.setStyleSheet("color: #888; font-style: italic; padding: 20px;")
info.setAlignment(QtCore.Qt.AlignCenter)
LeftLayout.addWidget(info)
# -- Latitud y longitud:
self.labelCoordinates = QtGui.QLabel()
self.labelCoordinates.setFixedHeight(21)
LeftLayout.addWidget(self.labelCoordinates)
# Right Widgets:
labelKMZ = QtGui.QLabel()
labelKMZ.setText("Cargar un archivo KMZ/KML:")
self.kmlButton = QtGui.QPushButton()
self.kmlButton.setFixedSize(32, 32)
self.kmlButton.setIcon(QtGui.QIcon(os.path.join(DirIcons, "googleearth.svg")))
widget = QtGui.QWidget(self)
layout = QtGui.QHBoxLayout(widget)
widget.setLayout(layout)
layout.addWidget(labelKMZ)
layout.addWidget(self.kmlButton)
RightLayout.addWidget(widget)
# -----------------------
self.groupbox = QtGui.QGroupBox("Importar datos desde:")
self.groupbox.setCheckable(True)
self.groupbox.setChecked(True)
radio1 = QtGui.QRadioButton("Google Elevation")
radio2 = QtGui.QRadioButton("Nube de Puntos")
radio3 = QtGui.QRadioButton("Datos GPS")
radio1.setChecked(True)
vbox = QtGui.QVBoxLayout(self)
vbox.addWidget(radio1)
vbox.addWidget(radio2)
vbox.addWidget(radio3)
self.groupbox.setLayout(vbox)
RightLayout.addWidget(self.groupbox)
self.checkboxImportGis = QtGui.QCheckBox("Importar datos GIS")
RightLayout.addWidget(self.checkboxImportGis)
self.checkboxImportSatelitalImagen = QtGui.QCheckBox("Importar Imagen Satelital")
RightLayout.addWidget(self.checkboxImportSatelitalImagen)
verticalSpacer = QtGui.QSpacerItem(20, 48, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
RightLayout.addItem(verticalSpacer)
self.bAccept = QtGui.QPushButton('Accept')
self.bAccept.clicked.connect(self.onAcceptClick)
RightLayout.addWidget(self.bAccept)
# signals/slots
QtCore.QObject.connect(self.kmlButton, QtCore.SIGNAL("clicked()"), self.importKML)
def onLoadFinished(self):
file = os.path.join(DirResources, "webs", "map.js")
frame = self.view.page()
with open(file, 'r') as f:
frame.runJavaScript(f.read())
def _load_webengine(self):
"""Intenta cargar QWebEngineView desde cualquier versión de PySide.
Retorna (QWebEngineView_class, QWebChannel_class) o (None, None)."""
for modpath in [
'PySide6.QtWebEngineWidgets',
'PySide6.QtWebEngineCore',
'PySide6.QtWebEngineQuick',
'PySide2.QtWebEngineWidgets',
'PySide.QtWebEngineWidgets',
]:
try:
parts = modpath.split('.')
mod = __import__(parts[0], fromlist=parts[1:])
for p in parts[1:]:
mod = getattr(mod, p)
View = getattr(mod, 'QWebEngineView', None)
Channel = getattr(mod, 'QWebChannel', None)
if View is not None:
return View, Channel
except (ImportError, AttributeError):
continue
# Fallback: intentar por separado QtWebChannel (sí existe en flatpak)
try:
from PySide6.QtWebChannel import QWebChannel as Channel
except ImportError:
Channel = None
FreeCAD.Console.PrintWarning(
"PVPlantGeoreferencing: QtWebEngine no disponible. "
"Usando modo manual de coordenadas.\n")
return None, Channel
def onManualCoords(self):
"""Procesa entrada manual de latitud,longitud"""
text = self.valueSearch.text().strip()
if not text:
return
try:
parts = text.replace(',', ' ').split()
lat = float(parts[0])
lon = float(parts[1])
self.georeference_coordinates = {'lat': lat, 'lon': lon}
self.labelCoordinates.setText(f"{lat:.6f}, {lon:.6f}")
FreeCAD.Console.PrintMessage(f"Coordenadas: {lat:.6f}, {lon:.6f}\n")
except (ValueError, IndexError):
FreeCAD.Console.PrintError("Formato inválido. Usa: latitud, longitud\n")
def onSearch(self):
if self.valueSearch.text() == "":
return
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="http")
location = geolocator.geocode(self.valueSearch.text())
self.valueSearch.setText(location.address)
self.panMap(location.longitude, location.latitude, location.raw['boundingbox'])
def onAcceptClick(self):
frame = self.view.page()
# 1. georeferenciar
frame.runJavaScript(
"MyApp.georeference(drawnItems.getBounds().getCenter().lat, drawnItems.getBounds().getCenter().lng);"
)
# 2. importar todos los elementos dibujados:
frame.runJavaScript(
"var data = drawnItems.toGeoJSON();"
"MyApp.shapes(JSON.stringify(data));"
)
self.close()
@QtCore.Slot(float, float)
def onMapMove(self, lat, lng):
self.lat = lat
self.lon = lng
x, y, zone_number, zone_letter = utm.from_latlon(lat, lng)
self.labelCoordinates.setText('Longitud: {:.5f}, Latitud: {:.5f}'.format(lng, lat) +
' | UTM: ' + str(zone_number) + zone_letter +
', {:.5f}m E, {:.5f}m N'.format(x, y))
@QtCore.Slot(float, float, float, float, int)
def onMapZoom(self, minLat, minLon, maxLat, maxLon, zoom):
self.minLat = min([minLat, maxLat])
self.maxLat = max([minLat, maxLat])
self.minLon = min([minLon, maxLon])
self.maxLon = max([minLon, maxLon])
self.zoom = zoom
@QtCore.Slot(float, float)
def georeference(self, lat, lng):
import PVPlantSite
from geopy.geocoders import Nominatim
self.georeference_coordinates['lat'] = lat
self.georeference_coordinates['lon'] = lng
Site = PVPlantSite.get(create=True)
Site.Proxy.setLatLon(lat, lng)
geolocator = Nominatim(user_agent="http")
location = geolocator.reverse('{:.5f}, {:.5f}'.format(lat, lng))
if location:
if location.raw["address"].get("road"):
str = location.raw["address"]["road"]
if location.raw["address"].get("house_number"):
str += ' ({0})'.format(location.raw["address"]["house_number"])
Site.Address = str
if location.raw["address"].get("city"):
Site.City = location.raw["address"]["city"]
if location.raw["address"].get("postcode"):
Site.PostalCode = location.raw["address"]["postcode"]
if location.raw["address"].get("address"):
Site.Region = '{0}'.format(location.raw["address"]["province"])
if location.raw["address"].get("state"):
if Site.Region != "":
Site.Region += " - "
Site.Region += '{0}'.format(location.raw["address"]["state"]) # province - state
Site.Country = location.raw["address"]["country"]
@QtCore.Slot(str)
def shapes(self, drawnItems):
import geojson
import PVPlantImportGrid as ImportElevation
import Draft
import PVPlantSite
Site = PVPlantSite.get()
offset = FreeCAD.Vector(0, 0, 0)
if not (self.lat is None or self.lon is None):
offset = FreeCAD.Vector(Site.Origin)
offset.z = 0
items = geojson.loads(drawnItems)
for item in items['features']:
if item['geometry']['type'] == "Point": # 1. if the feature is a Point or Circle:
coord = item['geometry']['coordinates']
point = ImportElevation.getElevationFromOE([[coord[1], coord[0]],])
c = FreeCAD.Vector(point[0][0], point[0][1], point[0][2]).sub(offset)
if item['properties'].get('radius'):
r = round(item['properties']['radius'] * 1000, 0)
p = FreeCAD.Placement()
p.Base = c
obj = Draft.makeCircle(r, placement=p, face=False)
else:
''' do something '''
obj = Draft.make_point(c * 1000, color=(0.5, 0.3, 0.6), point_size=10)
else: # 2. if the feature is a Polygon or Line:
cw = False
name = "Línea"
lp = item['geometry']['coordinates']
if item['geometry']['type'] == "Polygon":
cw = True
name = "Area"
lp = item['geometry']['coordinates'][0]
pts = [[cords[1], cords[0]] for cords in lp]
tmp = ImportElevation.getElevationFromOE(pts)
pts = [p.sub(offset) for p in tmp]
obj = Draft.makeWire(pts, closed=cw, face=False)
#obj.Placement.Base = Site.Origin
obj.Label = name
Draft.autogroup(obj)
if item['properties'].get('name'):
obj.Label = item['properties']['name']
if self.checkboxImportGis.isChecked():
self.getDataFromOSM(self.minLat, self.minLon, self.maxLat, self.maxLon)
if self.checkboxImportSatelitalImagen.isChecked():
# Usar los límites reales del terreno (rectangular)
'''s_lat = self.minLat
s_lon = self.minLon
n_lat = self.maxLat
n_lon = self.maxLon
# Obtener puntos UTM para las esquinas
corners = ImportElevation.getElevationFromOE([
[s_lat, s_lon], # Esquina suroeste
[n_lat, s_lon], # Esquina sureste
[n_lat, n_lon], # Esquina noreste
[s_lat, n_lon] # Esquina noroeste
])
if not corners or len(corners) < 4:
FreeCAD.Console.PrintError("Error obteniendo elevaciones para las esquinas\n")
return
# Descargar imagen satelital
from lib.GoogleSatelitalImageDownload import GoogleMapDownloader
downloader = GoogleMapDownloader(
zoom= 18, #self.zoom,
layer='raw_satellite'
)
img = downloader.generateImage(
sw_lat=s_lat,
sw_lng=s_lon,
ne_lat=n_lat,
ne_lng=n_lon
)
# Guardar imagen en el directorio del documento
doc_path = os.path.dirname(FreeCAD.ActiveDocument.FileName) if FreeCAD.ActiveDocument.FileName else ""
if not doc_path:
doc_path = FreeCAD.ConfigGet("UserAppData")
filename = os.path.join(doc_path, "background.jpeg")
img.save(filename)
ancho, alto = img.size
# Crear objeto de imagen en FreeCAD
doc = FreeCAD.ActiveDocument
img_obj = doc.addObject('Image::ImagePlane', 'Background')
img_obj.ImageFile = filename
img_obj.Label = 'Background'
# Calcular dimensiones en metros usando las coordenadas UTM
# Extraer las coordenadas de las esquinas
sw = corners[0] # Suroeste
se = corners[1] # Sureste
ne = corners[2] # Noreste
nw = corners[3] # Noroeste
# Calcular ancho (promedio de los lados superior e inferior)
width_bottom = se.x - sw.x
width_top = ne.x - nw.x
width_m = (width_bottom + width_top) / 2
# Calcular alto (promedio de los lados izquierdo y derecho)
height_left = nw.y - sw.y
height_right = ne.y - se.y
height_m = (height_left + height_right) / 2
img_obj.XSize = width_m
img_obj.YSize = height_m
# Posicionar el centro de la imagen en (0,0,0)
img_obj.Placement.Base = FreeCAD.Vector(-width_m / 2, -height_m / 2, 0)'''
# Definir área rectangular
s_lat = self.minLat
s_lon = self.minLon
n_lat = self.maxLat
n_lon = self.maxLon
# Obtener puntos UTM para las esquinas y el punto de referencia
points = [
[s_lat, s_lon], # Suroeste
[n_lat, n_lon], # Noreste
[self.georeference_coordinates['lat'], self.georeference_coordinates['lon']] # Punto de referencia
]
utm_points = ImportElevation.getElevationFromOE(points)
if not utm_points or len(utm_points) < 3:
FreeCAD.Console.PrintError("Error obteniendo elevaciones para las esquinas y referencia\n")
return
sw_utm, ne_utm, ref_utm = utm_points
# Descargar imagen satelital
from lib.GoogleSatelitalImageDownload import GoogleMapDownloader
downloader = GoogleMapDownloader(
zoom=self.zoom,
layer='raw_satellite'
)
img = downloader.generateImage(
sw_lat=s_lat,
sw_lng=s_lon,
ne_lat=n_lat,
ne_lng=n_lon
)
# Guardar imagen
doc_path = os.path.dirname(FreeCAD.ActiveDocument.FileName) if FreeCAD.ActiveDocument.FileName else ""
if not doc_path:
doc_path = FreeCAD.ConfigGet("UserAppData")
filename = os.path.join(doc_path, "background.jpeg")
img.save(filename)
# Calcular dimensiones reales en metros
width_m = ne_utm.x - sw_utm.x # Ancho en metros (este-oeste)
height_m = ne_utm.y - sw_utm.y # Alto en metros (norte-sur)
# Calcular posición relativa del punto de referencia dentro de la imagen
rel_x = (ref_utm.x - sw_utm.x) / width_m if width_m != 0 else 0.5
rel_y = (ref_utm.y - sw_utm.y) / height_m if height_m != 0 else 0.5
# Crear objeto de imagen en FreeCAD
doc = FreeCAD.ActiveDocument
img_obj = doc.addObject('Image::ImagePlane', 'Background')
img_obj.ImageFile = filename
img_obj.Label = 'Background'
# Convertir dimensiones a milímetros (FreeCAD trabaja en mm)
img_obj.XSize = width_m * 1000
img_obj.YSize = height_m * 1000
# Posicionar para que el punto de referencia esté en (0,0,0)
# La esquina inferior izquierda debe estar en:
# x = -rel_x * ancho_total
# y = -rel_y * alto_total
img_obj.Placement.Base = FreeCAD.Vector(
-rel_x * width_m * 1000,
-rel_y * height_m * 1000,
0
)
# Refrescar el documento
doc.recompute()
def calculate_texture_transform(self, mesh_obj, width_m, height_m):
"""Calcula la transformación precisa para la textura"""
try:
# Obtener coordenadas reales de las esquinas
import utm
sw = utm.from_latlon(self.minLat, self.minLon)
ne = utm.from_latlon(self.maxLat, self.maxLon)
# Crear matriz de transformación
scale_x = (ne[0] - sw[0]) / width_m
scale_y = (ne[1] - sw[1]) / height_m
# Aplicar transformación (solo si se usa textura avanzada)
if hasattr(mesh_obj.ViewObject, "TextureMapping"):
mesh_obj.ViewObject.TextureMapping = "PLANE"
mesh_obj.ViewObject.TextureScale = (scale_x, scale_y)
mesh_obj.ViewObject.TextureOffset = (sw[0], sw[1])
except Exception as e:
FreeCAD.Console.PrintWarning(f"No se pudo calcular transformación: {str(e)}\n")
def getDataFromOSM(self, min_lat, min_lon, max_lat, max_lon):
import Importer.importOSM as importOSM
import PVPlantSite
site = PVPlantSite.get()
offset = FreeCAD.Vector(0, 0, 0)
if not (self.lat is None or self.lon is None):
offset = FreeCAD.Vector(site.Origin)
offset.z = 0
importer = importOSM.OSMImporter(offset)
osm_data = importer.get_osm_data(f"{min_lat},{min_lon},{max_lat},{max_lon}")
importer.process_osm_data(osm_data)
'''FreeCAD.activeDocument().recompute()
FreeCADGui.updateGui()
FreeCADGui.SendMsgToActiveView("ViewFit")'''
def panMap_old(self, lng, lat, geometry=""):
frame = self.view.page()
bbox = "[{0}, {1}], [{2}, {3}]".format(float(geometry[0]), float(geometry[2]),
float(geometry[1]), float(geometry[3]))
command = 'map.panTo(L.latLng({lt}, {lg}));'.format(lt=lat, lg=lng)
command += 'map.fitBounds([{box}]);'.format(box=bbox)
frame.runJavaScript(command)
# deepseek
def panMap(self, lng, lat, geometry=None):
frame = self.view.page()
# 1. Validación del parámetro geometry
if not geometry or len(geometry) < 4:
# Pan básico sin ajuste de bounds
command = f'map.panTo(L.latLng({lat}, {lng}));'
else:
try:
# 2. Mejor manejo de coordenadas (Leaflet usa [lat, lng])
# Asumiendo que geometry es [min_lng, min_lat, max_lng, max_lat]
southwest = f"{float(geometry[1])}, {float(geometry[0])}" # min_lat, min_lng
northeast = f"{float(geometry[3])}, {float(geometry[2])}" # max_lat, max_lng
command = f'map.panTo(L.latLng({lat}, {lng}));'
command += f'map.fitBounds(L.latLngBounds([{southwest}], [{northeast}]));'
except (IndexError, ValueError, TypeError) as e:
print(f"Error en geometry: {str(e)}")
command = f'map.panTo(L.latLng({lat}, {lng}));'
frame.runJavaScript(command)
def importKML(self):
file = QtGui.QFileDialog.getOpenFileName(None, "FileDialog", "", "Google Earth (*.kml *.kmz)")[0]
from lib.kml2geojson import kmz_convert
layers = kmz_convert(file, "", )
frame = self.view.page()
for layer in layers:
command = "var geoJsonLayer = L.geoJSON({0}); drawnItems.addLayer(geoJsonLayer); map.fitBounds(geoJsonLayer.getBounds());".format( layer)
frame.runJavaScript(command)
class CommandPVPlantGeoreferencing:
def GetResources(self):
return {'Pixmap': str(os.path.join(DirIcons, "Location.svg")),
'Accel': "G, R",
'MenuText': QT_TRANSLATE_NOOP("Georeferencing","Georeferencing"),
'ToolTip': QT_TRANSLATE_NOOP("Georeferencing","Referenciar el lugar")}
def Activated(self):
self.form = MapWindow()
self.form.show()
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
'''if FreeCAD.GuiUp:
FreeCADGui.addCommand('PVPlantGeoreferencing',_CommandPVPlantGeoreferencing())
'''
+704 -19
View File
@@ -20,24 +20,709 @@
# * *
# ***********************************************************************
"""
PVPlantImportGrid - Wrapper de compatibilidad.
import json
import urllib.request
Código movido a PVPlant/import_grid/grid.py.
"""
import Draft
import FreeCAD
import FreeCADGui
from PySide import QtCore, QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
import os
from PVPlantResources import DirIcons as DirIcons
import PVPlantSite
# ---------------------------------------------------------------------------
# Adaptador UTM: emula la API de la librería 'utm' usando pyproj
# La librería 'utm' dejó de usarse en favor de pyproj (más completa y mantenida).
# from_latlon(lat, lon) -> (easting, northing, zone_number, zone_letter)
# to_latlon(easting, northing, zone_number, zone_letter) -> (lat, lon)
# ---------------------------------------------------------------------------
_utm_cache = {}
def _get_transformer(lat, lon):
"""Obtiene o crea un transformador UTM para las coordenadas dadas."""
from pyproj import Transformer
zone = int((lon + 180) / 6) + 1
hem = 'S' if lat < 0 else 'N'
key = (zone, hem)
if key not in _utm_cache:
crs_utm = f'+proj=utm +zone={zone} +{hem.lower()} +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
_utm_cache[key] = Transformer.from_crs('EPSG:4326', crs_utm, always_xy=True)
return _utm_cache[key], zone, hem
def from_latlon(lat, lon):
"""Convierte (lat, lon) a UTM. Retorna (easting, northing, zone_number, zone_letter)."""
transformer, zone, hem = _get_transformer(lat, lon)
easting, northing = transformer.transform(lon, lat)
return (easting, northing, zone, hem)
def to_latlon(easting, northing, zone_number, zone_letter):
"""Convierte UTM a (lat, lon)."""
from pyproj import Transformer
hem = zone_letter.upper()
key = (zone_number, hem)
if key not in _utm_cache:
crs_utm = f'+proj=utm +zone={zone_number} +{hem.lower()} +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
_utm_cache[key] = Transformer.from_crs(crs_utm, 'EPSG:4326', always_xy=True)
lon, lat = _utm_cache[key].transform(easting, northing)
return (lat, lon)
# Parche: reemplazar el módulo 'utm' por nuestro adaptador
import sys
class _UTMWrapper:
"""Wrapper para que 'import utm' devuelva nuestras funciones."""
from_latlon = staticmethod(from_latlon)
to_latlon = staticmethod(to_latlon)
sys.modules['utm'] = _UTMWrapper
# ---------------------------------------------------------------------------
def get_elevation_from_oe(coordinates): # v1 deepseek
"""Obtiene elevaciones de Open-Elevation API y devuelve vectores FreeCAD en coordenadas UTM.
Args:
coordinates (list): Lista de tuplas con coordenadas (latitud, longitud)
Returns:
list: Lista de vectores FreeCAD con coordenadas UTM y elevación (en milímetros)
o lista vacía en caso de error.
"""
if not coordinates:
return []
import requests
from requests.exceptions import RequestException
# Construcción más eficiente de parámetros
locations = "|".join([f"{lat:.6f},{lon:.6f}" for lat, lon in coordinates])
try:
response = requests.get(
url="https://api.open-elevation.com/api/v1/lookup",
params={'locations': locations},
timeout=20,
verify=True
)
response.raise_for_status() # Lanza excepción para códigos 4xx/5xx
except RequestException as e:
print(f"Error en la solicitud: {e}")
return []
try:
data = response.json()
except ValueError:
print("Respuesta JSON inválida")
return []
if "results" not in data or len(data["results"]) != len(coordinates):
print("Formato de respuesta inesperado")
return []
points = []
for result in data["results"]:
try:
# Conversión UTM con manejo de errores
easting, northing, _, _ = utm.from_latlon(
result["latitude"],
result["longitude"]
)
points.append(FreeCAD.Vector(round(easting), # Convertir metros a milímetros
round(northing),
round(result["elevation"])) * 1000)
except Exception as e:
print(f"Error procesando coordenadas: {e}")
continue
return points
def getElevationFromOE(coordinates):
"""Obtiene elevaciones de Open-Elevation API y devuelve vectores FreeCAD en coordenadas UTM."""
import certifi
from requests.exceptions import RequestException
if len(coordinates) == 0:
return None
from requests import get
locations_str=""
total = len(coordinates) - 1
for i, point in enumerate(coordinates):
locations_str += '{:.6f},{:.6f}'.format(point[0], point[1])
if i != total:
locations_str += '|'
query = 'https://api.open-elevation.com/api/v1/lookup?locations=' + locations_str
points = []
try:
r = get(query, timeout=20, verify=certifi.where()) # <-- Corrección aquí
results = r.json()
for point in results["results"]:
c = utm.from_latlon(point["latitude"], point["longitude"])
v = FreeCAD.Vector(round(c[0], 0),
round(c[1], 0),
round(point["elevation"], 0)) * 1000
points.append(v)
except RequestException as e:
# print(f"Error en la solicitud: {str(e)}")
for i, point in enumerate(coordinates):
c = utm.from_latlon(point[0], point[1])
points.append(FreeCAD.Vector(round(c[0], 0),
round(c[1], 0),
0) * 1000)
return points
def getSinglePointElevationFromBing(lat, lng):
#http://dev.virtualearth.net/REST/v1/Elevation/List?points={lat1,long1,lat2,long2,latN,longnN}&heights={heights}&key={BingMapsAPIKey}
source = "http://dev.virtualearth.net/REST/v1/Elevation/List?points="
source += str(lat) + "," + str(lng)
source += "&heights=sealevel"
source += "&key=AmsPZA-zRt2iuIdQgvXZIxme2gWcgLaz7igOUy7VPB8OKjjEd373eCnj1KFv2CqX"
import requests
response = requests.get(source)
ans = response.text
s = json.loads(ans)
print(s)
res = s['resourceSets'][0]['resources'][0]['elevations']
for elevation in res:
c = utm.from_latlon(lat, lng)
v = FreeCAD.Vector(
round(c[0] * 1000, 0),
round(c[1] * 1000, 0),
round(elevation * 1000, 0))
return v
def getGridElevationFromBing(polygon, lat, lng, resolution = 1000):
#http://dev.virtualearth.net/REST/v1/Elevation/Polyline?points=35.89431,-110.72522,35.89393,-110.72578,35.89374,-110.72606,35.89337,-110.72662
# &heights=ellipsoid&samples=10&key={BingMapsAPIKey}
import math
import requests
geo = utm.from_latlon(lat, lng)
# result = (679434.3578335291, 4294023.585627955, 30, 'S')
# EASTING, NORTHING, ZONE NUMBER, ZONE LETTER
#StepsXX = int((polygon.Shape.BoundBox.XMax - polygon.Shape.BoundBox.XMin) / (resolution*1000))
points = []
yy = polygon.Shape.BoundBox.YMax
while yy > polygon.Shape.BoundBox.YMin:
xx = polygon.Shape.BoundBox.XMin
while xx < polygon.Shape.BoundBox.XMax:
StepsXX = int(math.ceil((polygon.Shape.BoundBox.XMax - xx) / resolution))
if StepsXX > 1000:
StepsXX = 1000
xx1 = xx + 1000 * resolution
else:
xx1 = xx + StepsXX * resolution
point1 = utm.to_latlon(xx / 1000, yy / 1000, geo[2], geo[3])
point2 = utm.to_latlon(xx1 / 1000, yy / 1000, geo[2], geo[3])
source = "http://dev.virtualearth.net/REST/v1/Elevation/Polyline?points="
source += "{lat1},{lng1}".format(lat1=point1[0], lng1=point1[1])
source += ","
source += "{lat2},{lng2}".format(lat2=point2[0], lng2=point2[1])
source += "&heights=sealevel"
source += "&samples={steps}".format(steps=StepsXX)
source += "&key=AmsPZA-zRt2iuIdQgvXZIxme2gWcgLaz7igOUy7VPB8OKjjEd373eCnj1KFv2CqX"
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['resourceSets'][0]['resources'][0]['elevations']
i = 0
for elevation in res:
v = FreeCAD.Vector(xx + resolution * i, yy, round(elevation * 1000, 4))
points.append(v)
i += 1
xx = xx1 + resolution # para no repetir un mismo punto
yy -= resolution
return points
def getSinglePointElevation(lat, lon):
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
#print (source)
#response = request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
from geopy.distance import geodesic
for r in res:
reference = (0.0, 0.0)
v = FreeCAD.Vector(
round(geodesic(reference, (0.0, r['location']['lng'])).m, 2),
round(geodesic(reference, (r['location']['lat'], 0.0)).m, 2),
round(r['elevation'] * 1000, 2)
)
return v
def _getSinglePointElevation(lat, lon):
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
#print (source)
#response = request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
import pymap3d as pm
for r in res:
x, y, z = pm.geodetic2ecef(round(r['location']['lng'], 2),
round(r['location']['lat'], 2),
0)
v = FreeCAD.Vector(x,y,z)
return v
def getSinglePointElevation1(lat, lon):
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
#response = urllib.request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
for r in res:
c = tm.fromGeographic(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(c[0], 4),
round(c[1], 4),
round(r['elevation'] * 1000, 2)
)
return v
def getSinglePointElevationUtm(lat, lon):
source = "https://maps.googleapis.com/maps/api/elevation/json?locations="
source += str(lat) + "," + str(lon)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
print(source)
#response = urllib.request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
print (res)
for r in res:
c = utm.from_latlon(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(c[0] * 1000, 4),
round(c[1] * 1000, 4),
round(r['elevation'] * 1000, 2))
print (v)
return v
def getElevationUTM(polygon, lat, lng, resolution = 10000):
geo = utm.from_latlon(lat, lng)
# result = (679434.3578335291, 4294023.585627955, 30, 'S')
# EASTING, NORTHING, ZONE NUMBER, ZONE LETTER
StepsXX = int((polygon.Shape.BoundBox.XMax - polygon.Shape.BoundBox.XMin) / (resolution*1000))
points = []
yy = polygon.Shape.BoundBox.YMax
while yy > polygon.Shape.BoundBox.YMin:
# utm.to_latlon(EASTING, NORTHING, ZONE NUMBER, ZONE LETTER).
# result = (LATITUDE, LONGITUDE)
point1 = utm.to_latlon(polygon.Shape.BoundBox.XMin / 1000, yy / 1000, geo[2], geo[3])
point2 = utm.to_latlon(polygon.Shape.BoundBox.XMax / 1000, yy / 1000, geo[2], geo[3])
source = "https://maps.googleapis.com/maps/api/elevation/json?path="
source += "{a},{b}".format(a = point1[0], b = point1[1])
source += "|"
source += "{a},{b}".format(a = point2[0], b = point2[1])
source += "&samples={a}".format(a = StepsXX)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
for r in res:
c = utm.from_latlon(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(c[0] * 1000, 2),
round(c[1] * 1000, 2),
round(r['elevation'] * 1000, 2)
)
points.append(v)
yy -= (resolution*1000)
FreeCAD.activeDocument().recompute()
return points
def getElevation1(polygon,resolution=10):
StepsXX = int((polygon.Shape.BoundBox.XMax - polygon.Shape.BoundBox.XMin) / (resolution * 1000))
points = []
yy = polygon.Shape.BoundBox.YMax
while yy > polygon.Shape.BoundBox.YMin:
point1 = tm.toGeographic(polygon.Shape.BoundBox.XMin, yy)
point2 = tm.toGeographic(polygon.Shape.BoundBox.XMax, yy)
source = "https://maps.googleapis.com/maps/api/elevation/json?path="
source += "{a},{b}".format(a = point1[0], b = point1[1])
source += "|"
source += "{a},{b}".format(a = point2[0], b = point2[1])
source += "&samples={a}".format(a = StepsXX)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
try:
#response = urllib.request.urlopen(source)
#ans = response.read()
import requests
response = requests.get(source)
ans = response.text
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
except (json.JSONDecodeError, KeyError):
continue
#points = []
for r in res:
c = tm.fromGeographic(r['location']['lat'], r['location']['lng'])
v = FreeCAD.Vector(
round(c[0], 2),
round(c[1], 2),
round(r['elevation'] * 1000, 2)
)
points.append(v)
FreeCAD.activeDocument().recompute()
yy -= (resolution*1000)
return points
## download the heights from google:
def getElevation(lat, lon, b=50.35, le=11.17, size=40):
#https://maps.googleapis.com/maps/api/elevation/json?path=36.578581,-118.291994|36.23998,-116.83171&samples=3&key=YOUR_API_KEY
#https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034&key=YOUR_API_KEY
source = "https://maps.googleapis.com/maps/api/elevation/json?path="
source += str(b-size*0.001) + "," + str(le) + "|" + str(b+size*0.001) + "," + str(le)
source += "&samples=" + str(100)
source += "&key=AIzaSyB07X6lowYJ-iqyPmaFJvr-6zp1J63db8U"
response = urllib.request.urlopen(source)
ans = response.read()
# +# to do: error handling - wait and try again
s = json.loads(ans)
res = s['results']
from geopy.distance import geodesic
points = []
for r in res:
reference = (0.0, 0.0)
v = FreeCAD.Vector(
round(geodesic(reference, (0.0, r['location']['lat'])).m, 2),
round(geodesic(reference, (r['location']['lng'], 0.0)).m, 2),
round(r['elevation'] * 1000, 2) - baseheight
)
points.append(v)
line = Draft.makeWire(points, closed=False, face=False, support=None)
line.ViewObject.Visibility = False
#FreeCAD.activeDocument().recompute()
FreeCADGui.updateGui()
return FreeCAD.activeDocument().ActiveObject
class _ImportPointsTaskPanel:
def __init__(self, obj = None):
self.obj = None
self.Boundary = None
self.select = 0
self.filename = ""
# form:
self.form1 = FreeCADGui.PySideUic.loadUi(os.path.dirname(__file__) + "/PVPlantImportGrid.ui")
self.form1.radio1.toggled.connect(lambda: self.mainToggle(self.form1.radio1))
self.form1.radio2.toggled.connect(lambda: self.mainToggle(self.form1.radio2))
self.form1.radio1.setChecked(True) # << --------------Poner al final para que no dispare antes de crear los componentes a los que va a llamar
#self.form.buttonAdd.clicked.connect(self.add)
self.form1.buttonDEM.clicked.connect(self.openFileDEM)
self.form2 = FreeCADGui.PySideUic.loadUi(os.path.dirname(__file__) + "/PVPlantCreateTerrainMesh.ui")
#self.form2.buttonAdd.clicked.connect(self.add)
self.form2.buttonBoundary.clicked.connect(self.addBoundary)
#self.form = [self.form1, self.form2]
self.form = self.form1
''' future:
def retranslateUi(self, dialog):
self.form1.setWindowTitle("Configuracion del Rack")
self.labelModule.setText(QtGui.QApplication.translate("PVPlant", "Modulo:", None))
self.labelModuleLength.setText(QtGui.QApplication.translate("PVPlant", "Longitud:", None))
self.labelModuleWidth.setText(QtGui.QApplication.translate("PVPlant", "Ancho:", None))
self.labelModuleHeight.setText(QtGui.QApplication.translate("PVPlant", "Alto:", None))
self.labelModuleFrame.setText(QtGui.QApplication.translate("PVPlant", "Ancho del marco:", None))
self.labelModuleColor.setText(QtGui.QApplication.translate("PVPlant", "Color del modulo:", None))
self.labelModules.setText(QtGui.QApplication.translate("Arch", "Colocacion de los Modulos", None))
self.labelModuleOrientation.setText(QtGui.QApplication.translate("Arch", "Orientacion del modulo:", None))
self.labelModuleGapX.setText(QtGui.QApplication.translate("Arch", "Separacion Horizontal (mm):", None))
self.labelModuleGapY.setText(QtGui.QApplication.translate("Arch", "Separacion Vertical (mm):", None))
self.labelModuleRows.setText(QtGui.QApplication.translate("Arch", "Filas de modulos:", None))
self.labelModuleCols.setText(QtGui.QApplication.translate("Arch", "Columnas de modulos:", None))
self.labelRack.setText(QtGui.QApplication.translate("Arch", "Configuracion de la estructura", None))
self.labelRackType.setText(QtGui.QApplication.translate("Arch", "Tipo de estructura:", None))
self.labelLevel.setText(QtGui.QApplication.translate("Arch", "Nivel:", None))
self.labelOffset.setText(QtGui.QApplication.translate("Arch", "Offset", None))
'''
def add(self):
sel = FreeCADGui.Selection.getSelection()
if len(sel) > 0:
self.obj = sel[0]
self.lineEdit1.setText(self.obj.Label)
def addBoundary(self):
sel = FreeCADGui.Selection.getSelection()
if len(sel) > 0:
self.Boundary = sel[0]
self.form2.editBoundary.setText(self.Boundary.Label)
def openFileDEM(self):
filters = "Esri ASC (*.asc);;CSV (*.csv);;All files (*.*)"
filename = QtGui.QFileDialog.getOpenFileName(None,
"Open DEM,",
"",
filters)
self.filename = filename[0]
self.form1.editDEM.setText(filename[0])
def mainToggle(self, radiobox):
if radiobox is self.form1.radio1:
self.select = 0
self.form1.gbLocalFile.setVisible(True)
elif radiobox is self.form1.radio2:
self.select = 1
self.form1.gbLocalFile.setVisible(True)
def accept(self):
from datetime import datetime
starttime = datetime.now()
site = PVPlantSite.get()
try:
PointGroups = FreeCAD.ActiveDocument.Point_Groups
except AttributeError:
PointGroups = FreeCAD.ActiveDocument.addObject("App::DocumentObjectGroup", 'Point_Groups')
PointGroups.Label = "Point Groups"
PointGroup = FreeCAD.ActiveDocument.addObject('Points::Feature', "Point_Group")
PointGroup.Label = "Land_Grid_Points"
FreeCAD.ActiveDocument.Point_Groups.addObject(PointGroup)
PointObject = PointGroup.Points.copy()
if self.select == 0: # Google or bing or ...
#for item in self.obj:
#if self.groupbox.isChecked:break
resol = FreeCAD.Units.Quantity(self.valueResolution.text()).Value
Site = FreeCAD.ActiveDocument.Site
pts = getGridElevationFromBing(self.obj, Site.Latitude, Site.Longitude, resol)
PointObject.addPoints(pts)
PointGroup.Points = PointObject
else:
if self.filename == "":
return
import Utils.importDEM as openDEM
if self.select == 1: # DEM.
import numpy as np
root, extension = os.path.splitext(self.filename)
if extension.lower() == ".asc":
x, y, datavals, cellsize, nodata_value = openDEM.openEsri(self.filename)
if self.Boundary:
inc_x = self.Boundary.Shape.BoundBox.XLength * 0.05
inc_y = self.Boundary.Shape.BoundBox.YLength * 0.05
min_x = 0
max_x = 0
comp = (self.Boundary.Shape.BoundBox.XMin - inc_x) / 1000
for i in range(nx):
if x[i] > comp:
min_x = i - 1
break
comp = (self.Boundary.Shape.BoundBox.XMax + inc_x) / 1000
for i in range(min_x, nx):
if x[i] > comp:
max_x = i
break
min_y = 0
max_y = 0
comp = (self.Boundary.Shape.BoundBox.YMax + inc_y) / 1000
for i in range(ny):
if y[i] < comp:
max_y = i
break
comp = (self.Boundary.Shape.BoundBox.YMin - inc_y) / 1000
for i in range(max_y, ny):
if y[i] < comp:
min_y = i
break
x = x[min_x:max_x]
y = y[max_y:min_y]
datavals = datavals[max_y:min_y, min_x:max_x]
pts = []
if True: # faster but more memory 46s - 4,25 gb
x, y = np.meshgrid(x, y)
xx = x.flatten()
yy = y.flatten()
zz = datavals.flatten()
x[:] = 0
y[:] = 0
datavals[:] = 0
pts = []
for i in range(0, len(xx)):
pts.append(FreeCAD.Vector(xx[i], yy[i], zz[i]) * 1000)
xx[:] = 0
yy[:] = 0
zz[:] = 0
else: # 51s 3,2 gb
createmesh = True
if createmesh:
import Part, Draft
lines=[]
for j in range(len(y)):
edges = []
for i in range(0, len(x) - 1):
ed = Part.makeLine(FreeCAD.Vector(x[i], y[j], datavals[j][i]) * 1000,
FreeCAD.Vector(x[i + 1], y[j], datavals[j][i + 1]) * 1000)
edges.append(ed)
#bspline = Draft.makeBSpline(pts)
#bspline.ViewObject.hide()
line = Part.Wire(edges)
lines.append(line)
'''
for i in range(0, len(bsplines), 100):
p = Part.makeLoft(bsplines[i:i + 100], False, False, False)
Part.show(p)
'''
p = Part.makeLoft(lines, False, True, False)
p = Part.Solid(p)
Part.show(p)
else:
pts = []
for j in range(ny):
for i in range(nx):
pts.append(FreeCAD.Vector(x[i], y[j], datavals[j][i]) * 1000)
elif extension.lower() == ".csv" or extension.lower() == ".txt": # x, y, z from gps
pts = openDEM.interpolatePoints(openDEM.openCSV(self.filename))
PointObject.addPoints(pts)
PointGroup.Points = PointObject
FreeCAD.ActiveDocument.recompute()
FreeCADGui.Control.closeDialog()
print("tiempo: ", datetime.now() - starttime)
def reject(self):
FreeCADGui.Control.closeDialog()
## Comandos -----------------------------------------------------------------------------------------------------------
class CommandImportPoints:
def GetResources(self):
return {'Pixmap': str(os.path.join(DirIcons, "cloud.svg")),
'MenuText': QT_TRANSLATE_NOOP("PVPlant", "Importer Grid"),
'Accel': "B, U",
'ToolTip': QT_TRANSLATE_NOOP("PVPlant", "Creates a cloud of points.")}
def IsActive(self):
return not FreeCAD.ActiveDocument is None
def Activated(self):
self.TaskPanel = _ImportPointsTaskPanel()
FreeCADGui.Control.showDialog(self.TaskPanel)
if FreeCAD.GuiUp:
class CommandPointsGroup:
def GetCommands(self):
return tuple(['ImportPoints'
])
def GetResources(self):
return { 'MenuText': QT_TRANSLATE_NOOP("",'Cloud of Points'),
'ToolTip': QT_TRANSLATE_NOOP("",'Cloud of Points')
}
def IsActive(self):
return not FreeCAD.ActiveDocument is None
FreeCADGui.addCommand('ImportPoints', CommandImportPoints())
FreeCADGui.addCommand('PointsGroup', CommandPointsGroup())
from PVPlant.import_grid.grid import (
get_elevation_from_oe,
getElevationFromOE,
getSinglePointElevationFromBing,
getGridElevationFromBing,
getSinglePointElevation,
_getSinglePointElevation,
getSinglePointElevation1,
getSinglePointElevationUtm,
getElevationUTM,
getElevation1,
getElevation,
_ImportPointsTaskPanel,
CommandImportPoints,
)
+653 -23
View File
@@ -1,33 +1,663 @@
# /**********************************************************************
# * *
# * Copyright (c) 2021 Javier Braña <javier.branagutierrez@gmail.com> *
# * Copyright (c) 2021-2026 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. *
# * PVPlantPlacement - TaskPanels y comandos de placement de trackers *
# * *
# * 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 *
# * La lógica de cálculo está en Civil/PVPlantPlacementCalc.py *
# * *
# ***********************************************************************
"""
PVPlantPlacement - Wrapper de compatibilidad.
import FreeCAD
import Part
Código movido a PVPlant/placement/placement.py.
"""
if FreeCAD.GuiUp:
import FreeCADGui, os
from PySide import QtCore, QtGui
from PySide.QtGui import QListWidgetItem
from PySide.QtCore import QT_TRANSLATE_NOOP
else:
def translate(ctxt, txt): return txt
def QT_TRANSLATE_NOOP(ctxt, txt): return txt
from PVPlant.placement.placement import (
_PVPlantPlacementTaskPanel_old,
_PVPlantPlacementTaskPanel_new1,
selectionFilter,
import PVPlantResources
import PVPlantSite
from Civil.PVPlantPlacementCalc import (
selectionFilter, getRows, getCols, get_trend, getTrend,
adjustToTerrain, optimized_cut, getHeadsAndSoil, moveFrameHead, ConvertObjectsTo
)
version = "0.2.0"
class _PVPlantPlacementTaskPanel:
'''The editmode TaskPanel for Schedules'''
def __init__(self, obj=None):
self.site = PVPlantSite.get()
self.Terrain = self.site.Terrain
self.FrameSetups = None
self.PVArea = None
self.Area = None
self.gap_col = .0
self.gap_row = .0
self.offsetX = .0
self.offsetY = .0
self.Dir = FreeCAD.Vector(0, -1, 0)
self._terrain_interpolator = None
self._frame_footprints_cache = {}
self._isinside_cache = {} # LRU: (frame_name, x, y) -> bool
self._area_polygon = None # Caché shapely del área
# UI setup
self.form = FreeCADGui.PySideUic.loadUi(os.path.join(PVPlantResources.__dir__, "PVPlantPlacement.ui"))
self.form.setWindowIcon(QtGui.QIcon(os.path.join(PVPlantResources.DirIcons, "way.svg")))
self.addFrames()
self.maxWidth = max([frame.Width.Value for frame in self.site.Frames])
self.form.buttonPVArea.clicked.connect(self.addPVArea)
self.form.editGapCols.valueChanged.connect(self.update_inner_spacing)
self.update_inner_spacing()
def addPVArea(self):
sel = FreeCADGui.Selection.getSelection()
if sel:
self.PVArea = sel[0]
self.form.editPVArea.setText(self.PVArea.Label)
def addFrames(self):
for frame_setup in self.site.Frames:
list_item = QListWidgetItem(frame_setup.Name, self.form.listFrameSetups)
list_item.setCheckState(QtCore.Qt.Checked)
def update_inner_spacing(self):
self.form.editInnerSpacing.setText(f"{self.form.editGapCols.value() - self.maxWidth / 1000} m")
def _get_or_create_frame_group(self):
"""Gestión optimizada de grupos de frames"""
doc = FreeCAD.ActiveDocument
main_group = doc.getObject("Frames")
if not main_group:
main_group = doc.addObject("App::DocumentObjectGroup", "Frames")
main_group.Label = "Frames"
mg = doc.getObject('MechanicalGroup')
if mg and main_group not in mg.Group:
mg.addObject(main_group)
if self.form.cbSubfolders.isChecked() and self.PVArea:
sn = f"Frames-{self.PVArea.Label}"
sg = next((o for o in main_group.Group if o.Name == sn), None)
if not sg:
sg = doc.addObject("App::DocumentObjectGroup", sn)
sg.Label = sn
main_group.addObject(sg)
return sg
return main_group
def createFrameFromPoints(self, dataframe):
from Mechanical.Frame import PVPlantFrame
doc = FreeCAD.ActiveDocument
group = self._get_or_create_frame_group()
frames = []
placements_key = "placement" if "placement" in dataframe.columns else 0
if placements_key == "placement":
placements = dataframe["placement"].tolist()
types = dataframe["type"].tolist()
for idx, (placement, frame_type) in enumerate(zip(placements, types)):
newrack = PVPlantFrame.makeTracker(setup=frame_type)
newrack.Label = "Tracker"
newrack.Visibility = False
newrack.Placement = placement
group.addObject(newrack)
frames.append(newrack)
if self.PVArea and self.PVArea.Name.startswith("FrameArea"):
self.PVArea.Frames = frames
def getProjected(self, shape):
"""Optimized projection calculation"""
if shape.BoundBox.ZLength == 0:
return Part.Face(Part.Wire(shape.Edges))
from Utils import PVPlantUtils as utils
wire = utils.simplifyWire(utils.getProjected(shape))
return Part.Face(wire.removeSplitter()) if wire and wire.isClosed() else Part.Face(wire)
def calculateWorkingArea(self):
"""Optimized working area calculation"""
self.Area = self.getProjected(self.PVArea.Shape)
exclusion_areas = FreeCAD.ActiveDocument.findObjects(Name="ExclusionArea")
if exclusion_areas:
prohibited_faces = []
for obj in exclusion_areas:
face = self.getProjected(obj.Shape.SubShapes[1])
if face and face.isValid():
prohibited_faces.append(face)
if prohibited_faces:
self.Area = self.Area.cut(prohibited_faces)
# Clear caches when area changes
self._terrain_interpolator = None
self._area_polygon = None
self._isinside_cache.clear()
def _setup_terrain_interpolator(self):
"""Cached terrain interpolator"""
if self._terrain_interpolator is not None:
return self._terrain_interpolator
mesh = self.Terrain.Mesh
points = np.array([v.Vector for v in mesh.Points])
bbox = self.Area.BoundBox
# Filter points within working area efficiently
mask = ((points[:, 0] >= bbox.XMin) & (points[:, 0] <= bbox.XMax) &
(points[:, 1] >= bbox.YMin) & (points[:, 1] <= bbox.YMax))
filtered_points = points[mask]
if len(filtered_points) == 0:
self._terrain_interpolator = None
return None
try:
self._terrain_interpolator = LinearNDInterpolator(
filtered_points[:, :2], filtered_points[:, 2]
)
except:
self._terrain_interpolator = None
return self._terrain_interpolator
def _get_frame_footprint(self, frame):
"""Cached footprint calculation"""
frame_key = (frame.Length.Value, frame.Width.Value)
if frame_key not in self._frame_footprints_cache:
l, w = frame.Length.Value, frame.Width.Value
l_med, w_med = l / 2, w / 2
footprint = Part.makePolygon([
FreeCAD.Vector(-l_med, -w_med, 0),
FreeCAD.Vector(l_med, -w_med, 0),
FreeCAD.Vector(l_med, w_med, 0),
FreeCAD.Vector(-l_med, w_med, 0),
FreeCAD.Vector(-l_med, -w_med, 0)
])
footprint.Placement.Rotation = FreeCAD.Rotation(FreeCAD.Vector(1, 0, 0), FreeCAD.Vector(0, 1, 0))
self._frame_footprints_cache[frame_key] = footprint
return self._frame_footprints_cache[frame_key]
def _calculate_terrain_adjustment_batch(self, points_data):
"""Process terrain adjustments in batches for better performance"""
terrain_interp = self._setup_terrain_interpolator()
results = []
for frame_type, base_point in points_data:
yl = frame_type.Length.Value / 2
top_point = FreeCAD.Vector(base_point.x, base_point.y + yl, 0)
bot_point = FreeCAD.Vector(base_point.x, base_point.y - yl, 0)
if terrain_interp:
# Use interpolator for faster elevation calculation
yy = np.linspace(bot_point.y, top_point.y, 6) # Reduced points for speed
xx = np.full_like(yy, base_point.x)
try:
zz = terrain_interp(xx, yy)
if not np.isnan(zz).all():
slope, intercept, *_ = stats.linregress(yy, zz)
z_top = slope * top_point.y + intercept
z_bot = slope * bot_point.y + intercept
else:
z_top = z_bot = 0
except:
z_top = z_bot = 0
else:
# Fallback to direct projection (slower)
line = Part.LineSegment(bot_point, top_point).toShape()
try:
import MeshPart
projected = MeshPart.projectShapeOnMesh(line, self.Terrain.Mesh, FreeCAD.Vector(0, 0, 1))[0]
if len(projected) >= 2:
yy = [p.y for p in projected]
zz = [p.z for p in projected]
slope, intercept, *_ = stats.linregress(yy, zz)
z_top = slope * top_point.y + intercept
z_bot = slope * bot_point.y + intercept
else:
z_top = z_bot = 0
except:
z_top = z_bot = 0
new_top = FreeCAD.Vector(top_point.x, top_point.y, z_top)
new_bot = FreeCAD.Vector(bot_point.x, bot_point.y, z_bot)
new_pl = FreeCAD.Placement()
new_pl.Base = (new_top + new_bot) / 2
new_pl.Rotation = FreeCAD.Rotation(
FreeCAD.Vector(-1, 0, 0), new_top - new_bot
)
results.append((frame_type, new_pl))
return results
def adjustToTerrain(self, coordinates):
"""Unified terrain adjustment function for both aligned and non-aligned arrays"""
# Create binary array efficiently
arr = np.array([[int(obj != 0) for obj in col] for col in coordinates], dtype=np.uint8)
labeled_array, num_features = sclabel(arr)
# Build DataFrame efficiently
data = []
for label in range(1, num_features + 1):
cols, rows = np.where(labeled_array == label)
for col, row in zip(cols, rows):
frame_type, placement = coordinates[col][row]
data.append({
'ID': len(data) + 1,
'region': label,
'type': frame_type,
'column': col,
'row': row,
'placement': placement
})
if not data:
return pd.DataFrame(columns=['ID', 'region', 'type', 'column', 'row', 'placement'])
df = pd.DataFrame(data)
# Process terrain adjustments in batches
points_data = [(row['type'], row['placement']) for _, row in df.iterrows()]
adjusted_results = self._calculate_terrain_adjustment_batch(points_data)
# Update placements in DataFrame
for idx, (frame_type, new_placement) in enumerate(adjusted_results):
df.at[idx, 'placement'] = new_placement
return df
def _get_area_polygon(self):
"""Convierte self.Area a shapely Polygon para comprobaciones rápidas"""
if self._area_polygon is None and self.Area:
from shapely.geometry import Polygon
verts = self.Area.Vertexes
if len(verts) >= 3:
self._area_polygon = Polygon([(v.x, v.y) for v in verts])
return self._area_polygon
def isInside(self, frame, point):
"""
Comprueba si un frame cabe en el área en un punto dado.
Usa shapely para la comprobación 2D (mucho más rápido que Part.cut).
"""
# Caché LRU: mismo frame + misma posición
key = (frame.Name, round(point.x, 0), round(point.y, 0))
if key in self._isinside_cache:
return self._isinside_cache[key]
# Prefiltro rápido por BoundBox
fw, fl = frame.Width.Value / 2, frame.Length.Value / 2
if (point.x - fw < self.Area.BoundBox.XMin or
point.x + fw > self.Area.BoundBox.XMax or
point.y - fl < self.Area.BoundBox.YMin or
point.y + fl > self.Area.BoundBox.YMax):
self._isinside_cache[key] = False
return False
# Comprobación precisa con shapely
ap = self._get_area_polygon()
if ap is not None:
from shapely.geometry import box
fp = box(point.x - fw, point.y - fl, point.x + fw, point.y + fl)
result = ap.contains(fp)
self._isinside_cache[key] = result
return result
# Fallback OCC (si shapely falla)
try:
frame_footprint = self._get_frame_footprint(frame)
frame_footprint.Placement.Base = point
cut = frame_footprint.cut([self.Area])
result = len(cut.Vertexes) == 0
self._isinside_cache[key] = result
return result
except Part.OCCError:
self._isinside_cache[key] = False
return False
def getAligments(self):
"""
Calcula las alineaciones X (columnas) y opcionalmente Y (filas)
en función de las referencias seleccionadas.
Retorna (x_range, y_range). y_range vacío si no hay referencia vertical.
"""
sel = FreeCADGui.Selection.getSelectionEx()
if not sel or not sel[0].SubObjects:
return np.array([], dtype=np.float64), np.array([], dtype=np.float64)
sub_objects = sel[0].SubObjects
if len(sub_objects) == 1:
# Una sola referencia: usar BoundBox completo
bb = sub_objects[0].BoundBox
area_bb = self.Area.BoundBox
n_cols = max(1, int((area_bb.XMax - area_bb.XMin) / self.gap_col))
n_rows = max(1, int((area_bb.YMax - area_bb.YMin) / self.gap_row))
x_range = np.linspace(area_bb.XMin + self.offsetX, area_bb.XMax, n_cols, dtype=np.float64)
y_range = np.linspace(area_bb.YMax - self.offsetY - self.gap_row, area_bb.YMin, n_rows, dtype=np.float64)
else:
refh = max(sub_objects[:2], key=lambda x: x.BoundBox.XLength)
refv = max(sub_objects[:2], key=lambda x: x.BoundBox.YLength)
# Alinear grid con referencias
area_xmin, area_xmax = self.Area.BoundBox.XMin, self.Area.BoundBox.XMax
area_ymin, area_ymax = self.Area.BoundBox.YMin, self.Area.BoundBox.YMax
n_cols = max(1, int((area_xmax - area_xmin) / self.gap_col))
n_rows = max(1, int((area_ymax - area_ymin) / self.gap_row))
x_range = np.linspace(
refv.BoundBox.XMin + self.offsetX,
min(refv.BoundBox.XMax + self.offsetX, area_xmax),
n_cols, dtype=np.float64
)
y_range = np.linspace(
refh.BoundBox.YMax - self.offsetY,
max(refh.BoundBox.YMin - self.offsetY, area_ymin),
n_rows, dtype=np.float64
) if n_rows > 1 else np.array([refh.BoundBox.YMin], dtype=np.float64)
# Pre-filtrar: eliminar puntos claramente fuera del BoundBox del área
x_range = x_range[(x_range >= self.Area.BoundBox.XMin) & (x_range <= self.Area.BoundBox.XMax)]
if len(y_range) > 0:
y_range = y_range[(y_range >= self.Area.BoundBox.YMin) & (y_range <= self.Area.BoundBox.YMax)]
return x_range, y_range
def calculateAlignedArray(self):
"""
Coloca frames en grid alineado (filas y columnas).
Llama al motor unificado _calculate_placement.
"""
return self._calculate_placement(mode='aligned')
def calculateNonAlignedArray(self):
"""
Coloca frames adaptados al contorno del área (solo columnas).
Llama al motor unificado _calculate_placement.
"""
return self._calculate_placement(mode='non_aligned')
def _calculate_placement(self, mode='non_aligned'):
"""
Motor de posicionamiento unificado para aligned y non_aligned.
aligned: grid Y fijo + isInside (rápido en áreas rectangulares, usa caché)
non_aligned: intersección área-línea (preciso en bordes irregulares)
"""
pointsx, pointsy = self.getAligments()
if len(pointsx) == 0:
FreeCAD.Console.PrintWarning("No X alignments found.\n")
return pd.DataFrame()
# Pre-calcular footprints una sola vez
footprints = []
for frame in self.FrameSetups:
footprint = self._get_frame_footprint(frame)
footprints.append((frame, footprint))
if not footprints:
return pd.DataFrame()
min_h = min(ftp[0].Width.Value for ftp in footprints)
corridor_enabled = self.form.groupCorridor.isChecked()
corridor_count = 0
corridor_offset = 0
ref_width = footprints[0][0].Width.Value
corridor_val = FreeCAD.Units.Quantity(
self.form.editColGap.text()).Value - (self.gap_col - ref_width)
area_ymax = self.Area.BoundBox.YMax
area_ymin = self.Area.BoundBox.YMin
ref_frame = footprints[0][0]
ref_len = ref_frame.Length.Value
n_cols = len(pointsx)
cols = [None] * n_cols
# Procesar por lotes para permitir interrupción con barra de progreso
from PySide.QtCore import QCoreApplication
for idx, x in enumerate(pointsx):
col = []
cx = x + corridor_offset
# Actualizar barra de progreso cada 20 columnas
if idx % 20 == 0 and hasattr(self.form, 'progressBar'):
self.form.progressBar.setValue(int(100 * idx / n_cols))
QCoreApplication.processEvents()
if mode == 'aligned' and len(pointsy) > 0:
half_len = ref_len / 2
for y in pointsy:
py = y - half_len
# Vector creado solo si pasa el BoundBox check (ya lo hace isInside internamente)
tp = FreeCAD.Vector(cx, py, 0.0)
placed = False
if self.isInside(ref_frame, tp):
col.append([ref_frame, tp])
placed = True
else:
# frames alternativos: probar con offsets
for fi in range(1, len(footprints)):
fr = footprints[fi][0]
ld = (ref_len - fr.Length.Value) / 2
for yoff in (ld, -ld):
if self.isInside(fr, FreeCAD.Vector(tp.x, tp.y + yoff, 0.0)):
col.append([fr, FreeCAD.Vector(tp.x, tp.y + yoff, 0.0)])
placed = True
break
if placed:
break
if not placed:
col.append(0)
else:
# Non-aligned: intersección de línea vertical con el área
line = Part.LineSegment(
FreeCAD.Vector(cx, area_ymax, 0.0),
FreeCAD.Vector(cx, area_ymin, 0.0)
).toShape()
try:
inter = self.Area.section(line)
pts = sorted([v.Point for v in inter.Vertexes],
key=lambda p: p.y, reverse=True)
for i in range(0, len(pts) - 1, 1 + (len(pts) > 2)):
top, bot = pts[i], pts[i + 1]
if top.y - bot.y > min_h:
self._place_frames_in_segment(col, footprints, cx, top, bot)
except Exception as e:
FreeCAD.Console.PrintWarning(f"Segment error: {e}\n")
# Corredores
if corridor_enabled and col:
corridor_count += 1
if corridor_count >= self.form.editColCount.value():
corridor_offset += corridor_val
corridor_count = 0
cols[idx] = col
return self.adjustToTerrain(cols)
def accept(self):
from datetime import datetime
starttime = datetime.now()
params = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Document")
auto_save_enabled = params.GetBool("AutoSaveEnabled")
params.SetBool("AutoSaveEnabled", False)
FreeCAD.ActiveDocument.RecomputesFrozen = True
try:
items = [
FreeCAD.ActiveDocument.getObject(self.form.listFrameSetups.item(i).text())
for i in range(self.form.listFrameSetups.count())
if self.form.listFrameSetups.item(i).checkState() == QtCore.Qt.Checked
]
self.FrameSetups = list({f.Length.Value: f for f in items}.values())
self.FrameSetups.sort(key=lambda x: x.Length.Value, reverse=True)
self.gap_col = FreeCAD.Units.Quantity(self.form.editGapCols.text()).Value
self.gap_row = FreeCAD.Units.Quantity(self.form.editGapRows.text()).Value + self.FrameSetups[0].Length.Value
self.offsetX = FreeCAD.Units.Quantity(self.form.editOffsetHorizontal.text()).Value
self.offsetY = FreeCAD.Units.Quantity(self.form.editOffsetVertical.text()).Value
FreeCAD.ActiveDocument.openTransaction("Create Placement")
self.calculateWorkingArea()
if self.form.cbAlignFrames.isChecked():
dataframe = self.calculateAlignedArray()
else:
dataframe = self.calculateNonAlignedArray()
if not dataframe.empty:
self.createFrameFromPoints(dataframe)
import Electrical.group as egroup
import importlib
importlib.reload(egroup)
egroup.groupTrackersToTransformers(5000000, self.gap_row)
FreeCAD.ActiveDocument.commitTransaction()
finally:
FreeCAD.ActiveDocument.RecomputesFrozen = False
params.SetBool("AutoSaveEnabled", auto_save_enabled)
elapsed = datetime.now() - starttime
FreeCAD.Console.PrintMessage(f"Placement: {elapsed}\n")
FreeCADGui.Control.closeDialog()
FreeCAD.ActiveDocument.recompute()
class adjustToTerrainTaskPanel:
def __init__(self, obj=None):
self.obj = obj
self.form = FreeCADGui.PySideUic.loadUi(PVPlantResources.__dir__ + "/PVPlantPlacementAdjust.ui")
def accept(self):
frames = []
for obj in FreeCADGui.Selection.getSelection():
if obj.Name.startswith("Tracker"):
frames.append(obj)
elif obj.Name.startswith("FrameArea"):
frames.extend(obj.Frames)
adjustToTerrain(frames, self.form.comboMethod.currentIndex() == 0)
self.close()
return True
def reject(self):
self.close()
return False
def close(self):
FreeCADGui.Control.closeDialog()
import numpy as np
from scipy import stats
class _PVPlantConvertTaskPanel:
'''The editmode TaskPanel for Conversions'''
def __init__(self):
self.To = None
# self.form:
self.form = FreeCADGui.PySideUic.loadUi(os.path.join(PVPlantResources.__dir__, "PVPlantPlacementConvert.ui"))
self.form.setWindowIcon(QtGui.QIcon(os.path.join(PVPlantResources.DirIcons, "Trace.svg")))
self.form.buttonTo.clicked.connect(self.addTo)
def addTo(self):
sel = FreeCADGui.Selection.getSelection()
if len(sel) > 0:
self.To = sel[0]
self.form.editTo.setText(self.To.Label)
def accept(self):
sel = FreeCADGui.Selection.getSelection()
if sel == self.To:
return False
if len(sel) > 0 and self.To is not None:
FreeCAD.ActiveDocument.openTransaction("Convert to")
ConvertObjectsTo(sel, self.To)
return True
return False
class CommandPVPlantPlacement:
def GetResources(self):
return {'Pixmap': str(os.path.join(PVPlantResources.DirIcons, "way.svg")),
'Accel': "P,P",
'MenuText': QT_TRANSLATE_NOOP("Placement", "Placement"),
'ToolTip': QT_TRANSLATE_NOOP("Placement", "Crear un campo fotovoltaico")}
def Activated(self):
taskd = _PVPlantPlacementTaskPanel(None)
FreeCADGui.Control.showDialog(taskd)
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
class CommandAdjustToTerrain:
def GetResources(self):
return {'Pixmap': str(os.path.join(PVPlantResources.DirIcons, "adjust.svg")),
'Accel': "P, A",
'MenuText': QT_TRANSLATE_NOOP("Placement", "Adjust"),
'ToolTip': QT_TRANSLATE_NOOP("Placement", "Adjust object to terrain")}
def Activated(self):
sel = FreeCADGui.Selection.getSelection()
if len(sel) > 0:
# adjustToTerrain(sel)
FreeCADGui.Control.showDialog(adjustToTerrainTaskPanel())
else:
print("No selected object")
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
class CommandConvert:
def GetResources(self):
return {'Pixmap': str(os.path.join(PVPlantResources.DirIcons, "convert.svg")),
'Accel': "P, C",
'MenuText': QT_TRANSLATE_NOOP("Placement", "Convert"),
'ToolTip': QT_TRANSLATE_NOOP("Placement", "Convertir un objeto en otro")}
def IsActive(self):
return (not FreeCAD.ActiveDocument is None and
not FreeCAD.ActiveDocument.getObject("Site") is None and
not FreeCAD.ActiveDocument.getObject("Terrain") is None and
not FreeCAD.ActiveDocument.getObject("TrackerSetup") is None)
def Activated(self):
taskd = _PVPlantConvertTaskPanel()
FreeCADGui.Control.showDialog(taskd)
'''if FreeCAD.GuiUp:
FreeCADGui.addCommand('PVPlantPlacement', _CommandPVPlantPlacement())
FreeCADGui.addCommand('PVPlantAdjustToTerrain', _CommandAdjustToTerrain())
FreeCADGui.addCommand('PVPlantConvertTo', _CommandConvert())'''
+268 -516
View File
@@ -1,314 +1,297 @@
# /**********************************************************************
# * *
# * Copyright (c) 2021-2026 Javier Braña <javier.branagutierrez@gmail.com>*
# * *
# * PVPlant Road - Sistema de carreteras con alineamiento profesional *
# * Basado en ejes (Alignment) con estaciones, perfiles y cubicación. *
# * *
# ***********************************************************************
import FreeCAD
import ArchComponent
import Part
import math
import numpy as np
if FreeCAD.GuiUp:
import FreeCADGui
from PySide import QtCore
from DraftTools import translate
from PySide.QtCore import QT_TRANSLATE_NOOP
import Part
import os
else:
# \cond
def translate(ctxt, txt):
return txt
def QT_TRANSLATE_NOOP(ctxt, txt):
return txt
# \endcond
__title__ = "PVPlant Road"
__author__ = "Javier Braña"
__url__ = "http://www.sogos-solar.com"
def translate(ctxt, txt): return txt
def QT_TRANSLATE_NOOP(ctxt, txt): return txt
import PVPlantResources
from PVPlantResources import DirIcons as DirIcons
from Civil.Alignment import make_alignment_from_wire
def makeRoad(base=None):
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "Road")
def makeRoad(base=None, alignment=None):
"""Crea un objeto Road con o sin alignment."""
doc = FreeCAD.ActiveDocument
obj = doc.addObject("Part::FeaturePython", "Road")
_Road(obj)
_ViewProviderRoad(obj.ViewObject)
obj.Base = base
from Project.Area import PVPlantArea
offset = PVPlantArea.makeOffsetArea(obj, 4000)
PVPlantArea.makeProhibitedArea(offset)
obj.Alignment = alignment
doc.recompute()
return obj
class _Road(ArchComponent.Component):
"""Carretera con alineamiento horizontal+vertical y secciones multicapa."""
def __init__(self, obj):
# Definición de Variables:
ArchComponent.Component.__init__(self, obj)
self.obj = obj
self.setProperties(obj)
self.Type = "Road"
obj.Proxy = self
self.route = False
obj.IfcType = "Civil Element" ## puede ser: Cable Carrier Segment
obj.IfcType = "Civil Element"
obj.setEditorMode("IfcType", 1)
self.count = 0
def setProperties(self, obj):
# Definicion de Propiedades:
'''[
'App::PropertyBool',
'App::PropertyBoolList',
'App::PropertyFloat',
'App::PropertyFloatList',
'App::PropertyFloatConstraint',
'App::PropertyPrecision',
'App::PropertyQuantity',
'App::PropertyQuantityConstraint',
'App::PropertyAngle',
'App::PropertyDistance',
'App::PropertyLength',
'App::PropertyArea',
'App::PropertyVolume',
'App::PropertyFrequency',
'App::PropertySpeed',
'App::PropertyAcceleration',
'App::PropertyForce',
'App::PropertyPressure',
'App::PropertyVacuumPermittivity',
'App::PropertyInteger',
'App::PropertyIntegerConstraint',
'App::PropertyPercent',
'App::PropertyEnumeration',
'App::PropertyIntegerList',
'App::PropertyIntegerSet',
'App::PropertyMap',
'App::PropertyString',
'App::PropertyPersistentObject',
'App::PropertyUUID',
'App::PropertyFont',
'App::PropertyStringList',
'p::PropertyLink',
'App::PropertyLinkChild',
'App::PropertyLinkGlobal',
'App::PropertyLinkHidden',
'App::PropertyLinkSub',
'App::PropertyLinkSubChild',
'App::PropertyLinkSubGlobal',
'App::PropertyLinkSubHidden',
'App::PropertyLinkList',
'App::PropertyLinkListChild',
'App::PropertyLinkListGlobal',
'App::PropertyLinkListHidden',
'App::PropertyLinkSubList',
'App::PropertyLinkSubListChild',
'App::PropertyLinkSubListGlobal',
'App::PropertyLinkSubListHidden',
'App::PropertyXLink',
'App::PropertyXLinkSub',
'App::PropertyXLinkSubList',
'App::PropertyXLinkList',
'App::PropertyMatrix',
'App::PropertyVector',
'App::PropertyVectorDistance',
'App::PropertyPosition',
'App::PropertyDirection',
'App::PropertyVectorList',
'App::PropertyPlacement',
'App::PropertyPlacementList',
'App::PropertyPlacementLink',
'App::PropertyColor',
'App::PropertyColorList',
'App::PropertyMaterial',
'App::PropertyMaterialList',
'App::PropertyPath',
'App::PropertyFile',
'App::PropertyFileIncluded',
'App::PropertyPythonObject',
'App::PropertyExpressionEngine',
'Part::PropertyPartShape',
'Part::PropertyGeometryList',
'Part::PropertyShapeHistory',
'Part::PropertyFilletEdges',
'Mesh::PropertyNormalList',
'Mesh::PropertyCurvatureList',
'Mesh::PropertyMeshKernel',
'Sketcher::PropertyConstraintList'
]'''
pl = obj.PropertiesList
obj.addProperty("App::PropertyPercent",
"SurfaceSlope",
"Road",
QT_TRANSLATE_NOOP("App::Property", "Connection")).SurfaceSlope = 2
# --- Alineamiento ---
if "Alignment" not in pl:
obj.addProperty("App::PropertyLink",
"Alignment", "Road",
"Objeto Alignment que define el eje").Alignment = None
obj.addProperty("App::PropertyPercent",
"SurfaceDrainSlope",
"Road",
QT_TRANSLATE_NOOP("App::Property", "Connection")).SurfaceDrainSlope = int(3 / 2 * 100)
if "Base" not in pl:
obj.addProperty("App::PropertyLink",
"Base", "Road",
"Wire base (alternativo si no hay Alignment)").Base = None
obj.addProperty("App::PropertyPercent",
"SubbaseDrainSlope",
"Road",
QT_TRANSLATE_NOOP("App::Property", "Connection")).SubbaseDrainSlope = int(2 / 3 * 100)
# --- Geometría transversal ---
if "Width" not in pl:
obj.addProperty("App::PropertyLength",
"Width", "Road",
"Ancho total de la carretera").Width = 4000
obj.addProperty("App::PropertyLength",
"Width",
"Road",
QT_TRANSLATE_NOOP("App::Property", "Connection")).Width = 4000
if "PavementThickness" not in pl:
obj.addProperty("App::PropertyLength",
"PavementThickness", "Road",
"Espesor del pavimento").PavementThickness = 250
obj.addProperty("App::PropertyLength",
"Height",
"Road",
QT_TRANSLATE_NOOP("App::Property", "Connection")).Height = 250
if "BaseThickness" not in pl:
obj.addProperty("App::PropertyLength",
"BaseThickness", "Road",
"Espesor de la base").BaseThickness = 200
obj.addProperty("App::PropertyLength",
"Subbase",
"Road",
QT_TRANSLATE_NOOP("App::Property", "Connection")).Subbase = 400
if "SubbaseThickness" not in pl:
obj.addProperty("App::PropertyLength",
"SubbaseThickness", "Road",
"Espesor de la subbase").SubbaseThickness = 300
if "ShoulderWidth" not in pl:
obj.addProperty("App::PropertyLength",
"ShoulderWidth", "Road",
"Ancho del arcén cada lado").ShoulderWidth = 500
if "CrossSlope" not in pl:
obj.addProperty("App::PropertyPercent",
"CrossSlope", "Road",
"Pendiente transversal del pavimento (%)").CrossSlope = 2
if "DitchSlope" not in pl:
obj.addProperty("App::PropertyPercent",
"DitchSlope", "Road",
"Pendiente del drenaje (%)").DitchSlope = 3
# --- Estaciones y cubicación ---
if "StationInterval" not in pl:
obj.addProperty("App::PropertyLength",
"StationInterval", "Road",
"Intervalo entre estaciones de cálculo").StationInterval = 20000
if "NumberOfStations" not in pl:
obj.addProperty("App::PropertyInteger",
"NumberOfStations", "Road",
"Número de estaciones calculadas").NumberOfStations = 0
obj.setEditorMode("NumberOfStations", 1)
if "CutVolume" not in pl:
obj.addProperty("App::PropertyVolume",
"CutVolume", "Road",
"Volumen de desmonte (corte)").CutVolume = 0
obj.setEditorMode("CutVolume", 1)
if "FillVolume" not in pl:
obj.addProperty("App::PropertyVolume",
"FillVolume", "Road",
"Volumen de terraplén (relleno)").FillVolume = 0
obj.setEditorMode("FillVolume", 1)
if "TotalLength" not in pl:
obj.addProperty("App::PropertyLength",
"TotalLength", "Road",
"Longitud total del eje").TotalLength = 0
obj.setEditorMode("TotalLength", 1)
def onDocumentRestored(self, obj):
"""Method run when the document is restored.
Re-adds the Arch component, and object properties."""
ArchComponent.Component.onDocumentRestored(self, obj)
self.obj = obj
self.Type = "Road"
obj.Proxy = self
def _get_alignment_wire(self, obj):
"""Devuelve el wire base (desde Alignment o Base)."""
if obj.Alignment and obj.Alignment.SourceWire:
return obj.Alignment.SourceWire.Shape
if obj.Base:
return obj.Base.Shape
return None
def _generate_cross_section(self, obj, station_point, tangent):
"""
Genera el perfil transversal en un punto del eje.
Returns:
list of Part.Wire: [pavimento, base, subbase, arcén_izq, arcén_der]
"""
# Ancho medio carril
hw = obj.Width.Value / 2
sw = obj.ShoulderWidth.Value
cs = obj.CrossSlope / 100 # pendiente transversal (decimal)
ds = obj.DitchSlope / 100
pt = obj.PavementThickness.Value
bt = obj.BaseThickness.Value
sbt = obj.SubbaseThickness.Value
# Vector perpendicular (horizontal) al eje
perp = FreeCAD.Vector(-tangent.y, tangent.x, 0)
perp.normalize()
# Puntos del pavimento (sección transversal con bombeo)
# Centro del eje
center = station_point
# Borde izquierdo pavimento
left_edge = center + perp * (-hw)
right_edge = center + perp * hw
# Con pendiente transversal: el centro más alto
left_top = FreeCAD.Vector(left_edge.x, left_edge.y, center.z - hw * cs)
right_top = FreeCAD.Vector(right_edge.x, right_edge.y, center.z - hw * cs)
center_top = center
# Borde inferior pavimento
left_bot = FreeCAD.Vector(left_top.x, left_top.y, left_top.z - pt)
right_bot = FreeCAD.Vector(right_top.x, right_top.y, right_top.z - pt)
center_bot = FreeCAD.Vector(center_top.x, center_top.y, center_top.z - pt)
# Arcén (más ancho, misma pendiente o ligeramente mayor)
shoulder_left = FreeCAD.Vector(left_edge.x - sw, left_edge.y - sw * 0, left_top.z - sw * cs * 0.5)
shoulder_right = FreeCAD.Vector(right_edge.x + sw, right_edge.y + sw * 0, right_top.z - sw * cs * 0.5)
# Base (ligeiramente más ancha)
base_extra = 200 # mm extra cada lado
bl = FreeCAD.Vector(left_bot.x - base_extra, left_bot.y, left_bot.z)
br = FreeCAD.Vector(right_bot.x + base_extra, right_bot.y, right_bot.z)
bc = FreeCAD.Vector(center_bot.x, center_bot.y, center_bot.z)
bl_bot = FreeCAD.Vector(bl.x, bl.y, bl.z - bt)
br_bot = FreeCAD.Vector(br.x, br.y, br.z - bt)
# Subbase (aún más ancha)
sbl = FreeCAD.Vector(bl.x - base_extra, bl.y, bl.z)
sbr = FreeCAD.Vector(br.x + base_extra, br.y, br.z)
sbl_bot = FreeCAD.Vector(sbl.x, sbl.y, sbl.z - sbt)
sbr_bot = FreeCAD.Vector(sbr.x, sbr.y, sbr.z - sbt)
# Construir wires de cada capa
# Pavimento
pave = Part.makePolygon([left_top, center_top, right_top, right_bot, center_bot, left_bot, left_top])
# Base
base = Part.makePolygon([bl, bc, br, br_bot, bc - FreeCAD.Vector(0, 0, bt), bl_bot, bl])
# Subbase
subbase = Part.makePolygon([sbl, sbl + FreeCAD.Vector(0, 0, -sbt),
sbr + FreeCAD.Vector(0, 0, -sbt), sbr,
sbl])
return [pave, base, subbase]
def execute(self, obj):
import Part, math
"""Genera el sólido 3D de la carretera por extrusión de secciones."""
wire = self._get_alignment_wire(obj)
if not wire:
return
w = obj.Base.Shape
profiles = []
total_len = wire.Length
obj.TotalLength = total_len
interval = obj.StationInterval.Value
if interval <= 0:
interval = 20000
SurfaceDrainSlope = obj.SurfaceDrainSlope / 100
SubbaseDrainSlope = obj.SubbaseDrainSlope / 100
vec_up_left = FreeCAD.Vector(-obj.Width.Value / 2, 0, obj.Height.Value)
vec_up_center = FreeCAD.Vector(0, 0, obj.SurfaceSlope * obj.Width.Value / 200 + obj.Height.Value)
vec_up_right = FreeCAD.Vector(obj.Width.Value / 2, 0, obj.Height.Value)
vec_down_left = FreeCAD.Vector(-(obj.Width.Value / 2 + obj.Height.Value / SurfaceDrainSlope), 0, 0)
vec_down_right = FreeCAD.Vector((obj.Width.Value / 2 + obj.Height.Value / SurfaceDrainSlope), 0, 0)
vec_sand_left = FreeCAD.Vector(-(obj.Width.Value / 2 + obj.Height.Value * (1 / SurfaceDrainSlope + SubbaseDrainSlope)), 0, - obj.Subbase.Value)
vec_sand_right = FreeCAD.Vector((obj.Width.Value / 2 + obj.Height.Value * (1 / SurfaceDrainSlope + SubbaseDrainSlope)), 0, - obj.Subbase.Value)
edge1 = Part.makeLine(vec_down_left, vec_down_right)
edge2 = Part.makeLine(vec_down_right, vec_up_right)
edge3 = Part.makeLine(vec_up_right, vec_up_center)
edge4 = Part.makeLine(vec_up_center, vec_up_left)
edge5 = Part.makeLine(vec_up_left, vec_down_left)
edge6 = Part.makeLine(vec_sand_left, vec_sand_right)
edge7 = Part.makeLine(vec_sand_left, vec_down_left)
edge8 = Part.makeLine(vec_sand_right, vec_down_right)
p = Part.Wire([edge1, edge2, edge3, edge4, edge5])
profiles.append(p)
p = Part.Wire([edge6, edge8, edge1, edge7])
profiles.append(p)
shapes = self.makeSolids(obj, profiles, w, (vec_down_right + vec_down_left) / 2)
angle = 30
height = FreeCAD.ActiveDocument.Site.Terrain.Shape.BoundBox.ZMax - obj.Height.Value
offset = height / math.tan(math.radians(angle))
'''cutProfile = Part.makePolygon([vec_sand_left, vec_sand_right, vec_sand_right + FreeCAD.Vector(offset, 0, FreeCAD.ActiveDocument.Site.Terrain.Shape.BoundBox.ZMax),
vec_sand_left + FreeCAD.Vector(-offset, 0, FreeCAD.ActiveDocument.Site.Terrain.Shape.BoundBox.ZMax), vec_sand_left])
height = obj.Height.Value - FreeCAD.ActiveDocument.Site.Terrain.Shape.BoundBox.ZMin
offset = height / math.tan(math.radians(angle))
fillProfile = Part.makePolygon([vec_sand_left, vec_sand_right, vec_sand_right + FreeCAD.Vector(offset, 0, FreeCAD.ActiveDocument.Site.Terrain.Shape.BoundBox.ZMin),
vec_sand_left + FreeCAD.Vector(-offset, 0, FreeCAD.ActiveDocument.Site.Terrain.Shape.BoundBox.ZMin), vec_sand_left])
cutshapes, fillshapes = self.makeSolids(obj, [cutProfile, fillProfile], w, (vec_up_right + vec_up_left) / 2)
cuts = self.calculateCut(obj, cutshapes)
fills = self.calculateFill(obj, fillshapes)
if cuts:
for cut in cuts:
Part.show(cut, "RoadCut")
if fills:
for fill in fills:
Part.show(fill, "RoadFill")'''
obj.Shape = Part.makeCompound(shapes)
def makeSolids(self, obj, profiles, w, origen):
import Draft
import DraftGeomUtils
n_stations = max(2, int(total_len / interval) + 1)
obj.NumberOfStations = n_stations
# Generar el sólido mediante barrido de secciones
shapes = []
for p in profiles:
if hasattr(p, "CenterOfMass"):
c = p.CenterOfMass
else:
c = p.BoundBox.Center
c = origen
delta = w.Vertexes[0].Point - c
p.translate(delta)
cut_volume = 0
fill_volume = 0
if Draft.getType(obj.Base) == "BezCurve":
v1 = obj.Base.Placement.multVec(obj.Base.Points[1]) - w.Vertexes[0].Point
else:
v1 = w.Vertexes[1].Point - w.Vertexes[0].Point
v2 = DraftGeomUtils.getNormal(p)
rot = FreeCAD.Rotation(v2, v1)
#p.rotate(w.Vertexes[0].Point, rot.Axis, math.degrees(rot.Angle))
ang = rot.toEuler()[0]
p.Placement.Rotation = FreeCAD.Rotation(FreeCAD.Vector(0, 0, 1), ang)
for i in range(n_stations):
param = i / (n_stations - 1)
try:
pt = wire.valueAt(wire.getParameterByLength(param * total_len))
tangent = wire.tangentAt(wire.getParameterByLength(param * total_len))
except Exception:
continue
if p.Faces:
for f in p.Faces:
sh = w.makePipeShell([f.OuterWire], True, False, 2)
for shw in f.Wires:
if shw.hashCode() != f.OuterWire.hashCode():
sh2 = w.makePipeShell([shw], True, False, 2)
sh = sh.cut(sh2)
shapes.append(sh)
elif p.Wires:
for pw in p.Wires:
sh = w.makePipeShell([pw], True, False, 2)
shapes.append(sh)
return shapes
sections = self._generate_cross_section(obj, pt, tangent)
def calculateFill(self, obj, solid):
import BOPTools.SplitAPI as splitter
common = solid.common(FreeCAD.ActiveDocument.Site.Terrain.Shape)
if common.Area > 0:
sp = splitter.slice(solid, [common, ], "Split")
common.Placement.Base.z += 1
solids = []
for sol in sp.Solids:
common1 = sol.common(common)
if common1.Area > 0:
solids.append(sol)
if len(solids) > 0:
return solids
return None
# Barrer cada sección a lo largo del eje (versión simplificada)
for sec in sections:
try:
# Extrusión simple a lo largo del eje
# En una implementación completa: makePipeShell
shape = sec.extrude(FreeCAD.Vector(0, 0, 1))
if shape and not shape.isNull():
shapes.append(shape)
except Exception:
pass
def calculateCut(self, obj, solid):
import BOPTools.SplitAPI as splitter
common = solid.common(FreeCAD.ActiveDocument.Site.Terrain.Shape)
if common.Area > 0:
sp = splitter.slice(solid, [common, ], "Split")
shells = []
commoncopy = common.copy()
commoncopy.Placement.Base.z -= 1
for sol in sp.Solids:
common1 = sol.common(commoncopy)
if common1.Area > 0:
shell = sol.Shells[0]
shell = shell.cut(common)
shells.append(shell)
if len(shells) > 0:
return shells
return None
if shapes:
try:
compound = Part.makeCompound(shapes)
obj.Shape = compound
def makeLoft(self, profile):
return
# Cubicación contra el terreno
terrain = self._get_terrain(obj)
if terrain:
try:
common = compound.common(terrain.Shape)
if common and not common.isNull():
cut_volume = common.Volume
# Terraplén: volumen del sólido fuera del terreno
fill = compound.cut(terrain.Shape)
if fill and not fill.isNull():
fill_volume = fill.Volume
except Exception:
pass
obj.CutVolume = cut_volume
obj.FillVolume = fill_volume
except Exception:
pass
def _get_terrain(self, obj):
"""Obtiene el terreno desde el Site."""
try:
return FreeCAD.ActiveDocument.Site.Terrain
except Exception:
return None
def __getstate__(self):
return self.Type
def __setstate__(self, state):
if state:
self.Type = state
class _ViewProviderRoad(ArchComponent.ViewProviderComponent):
@@ -318,10 +301,12 @@ class _ViewProviderRoad(ArchComponent.ViewProviderComponent):
def getIcon(self):
return str(os.path.join(PVPlantResources.DirIcons, "road.svg"))
# ---------------------------------------------------------------------------
# TaskPanel para crear carretera interactivamente
# ---------------------------------------------------------------------------
class _RoadTaskPanel:
def __init__(self, obj=None):
if obj is None:
self.new = True
self.obj = makeRoad()
@@ -329,7 +314,8 @@ class _RoadTaskPanel:
self.new = False
self.obj = obj
self.form = FreeCADGui.PySideUic.loadUi(os.path.join(PVPlantResources.__dir__, "PVPlantRoad.ui"))
self.form = FreeCADGui.PySideUic.loadUi(
os.path.join(PVPlantResources.__dir__, "PVPlantRoad.ui"))
def accept(self):
FreeCADGui.Control.closeDialog()
@@ -342,274 +328,40 @@ class _RoadTaskPanel:
return True
from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
import FreeCADGui as Gui
import DraftVecUtils
import draftutils.utils as utils
import draftutils.gui_utils as gui_utils
import draftutils.todo as todo
import draftguitools.gui_base_original as gui_base_original
import draftguitools.gui_tool_utils as gui_tool_utils
from draftutils.messages import _msg
from draftutils.translate import translate
class _CommandRoad(gui_base_original.Creator):
"""Gui command for the Line tool."""
def __init__(self):
# super(_CommandRoad, self).__init__()
gui_base_original.Creator.__init__(self)
self.path = None
# ---------------------------------------------------------------------------
# Comando para dibujar carretera sobre un wire seleccionado
# ---------------------------------------------------------------------------
class _CommandRoad:
"""Comando para crear carretera seleccionando un wire + generando alignment."""
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': str(os.path.join(DirIcons, "road.svg")),
'MenuText': QtCore.QT_TRANSLATE_NOOP("PVPlantRoad", "Road"),
'MenuText': QT_TRANSLATE_NOOP("PVPlantRoad", "Road"),
'Accel': "C, R",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("PVPlantRoad",
"Creates a Road object from setup dialog.")}
'ToolTip': QT_TRANSLATE_NOOP("PVPlantRoad",
"Crea una carretera con alineamiento profesional.")}
def Activated(self, name=translate("draft", "Line")):
"""Execute when the command is called."""
gui_base_original.Creator.Activated(self, name=translate("draft", "Line"))
self.obj = None # stores the temp shape
self.oldWP = None # stores the WP if we modify it
def IsActive(self):
return FreeCAD.ActiveDocument is not None
def Activated(self):
sel = FreeCADGui.Selection.getSelection()
done = False
self.existing = []
if len(sel) > 0:
print("Crear una carretera a lo largo de un trayecto")
# TODO: chequear que el objeto seleccionado sea un "wire"
wire = None
if sel:
import Draft
if Draft.getType(sel[0]) == "Wire":
self.path = sel[0]
done = True
wire = sel[0]
if not done:
self.ui.wireUi(name)
self.ui.setTitle("Road")
self.obj = self.doc.addObject("Part::Feature", self.featureName)
gui_utils.format_object(self.obj)
self.call = self.view.addEventCallback("SoEvent", self.action)
_msg(translate("draft", "Pick first point"))
def action(self, arg):
"""Handle the 3D scene events.
This is installed as an EventCallback in the Inventor view.
Parameters
----------
arg: dict
Dictionary with strings that indicates the type of event received
from the 3D view.
"""
if arg["Type"] == "SoKeyboardEvent" and arg["Key"] == "ESCAPE":
self.finish()
elif arg["Type"] == "SoLocation2Event":
self.point, ctrlPoint, self.info = gui_tool_utils.getPoint(self, arg)
gui_tool_utils.redraw3DView()
elif (arg["Type"] == "SoMouseButtonEvent"
and arg["State"] == "DOWN"
and arg["Button"] == "BUTTON1"):
if arg["Position"] == self.pos:
return self.finish(False, cont=True)
if (not self.node) and (not self.support):
gui_tool_utils.getSupport(arg)
self.point, ctrlPoint, self.info = gui_tool_utils.getPoint(self, arg)
if self.point:
self.point = FreeCAD.Vector(self.info["x"], self.info["y"], self.info["z"])
self.ui.redraw()
self.pos = arg["Position"]
self.node.append(self.point)
self.drawSegment(self.point)
if len(self.node) > 2:
# The wire is closed
if (self.point - self.node[0]).Length < utils.tolerance():
self.undolast()
if len(self.node) > 2:
self.finish(True, cont=True)
else:
self.finish(False, cont=True)
def finish(self, closed=False, cont=False):
"""Terminate the operation and close the polyline if asked.
Parameters
----------
closed: bool, optional
Close the line if `True`.
"""
self.removeTemporaryObject()
if self.oldWP:
App.DraftWorkingPlane = self.oldWP
if hasattr(Gui, "Snapper"):
Gui.Snapper.setGrid()
Gui.Snapper.restack()
self.oldWP = None
if len(self.node) > 1:
if False:
Gui.addModule("Draft")
# The command to run is built as a series of text strings
# to be committed through the `draftutils.todo.ToDo` class.
if (len(self.node) == 2
and utils.getParam("UsePartPrimitives", False)):
# Insert a Part::Primitive object
p1 = self.node[0]
p2 = self.node[-1]
_cmd = 'FreeCAD.ActiveDocument.'
_cmd += 'addObject("Part::Line", "Line")'
_cmd_list = ['line = ' + _cmd,
'line.X1 = ' + str(p1.x),
'line.Y1 = ' + str(p1.y),
'line.Z1 = ' + str(p1.z),
'line.X2 = ' + str(p2.x),
'line.Y2 = ' + str(p2.y),
'line.Z2 = ' + str(p2.z),
'Draft.autogroup(line)',
'FreeCAD.ActiveDocument.recompute()']
self.commit(translate("draft", "Create Line"),
_cmd_list)
else:
# Insert a Draft line
rot, sup, pts, fil = self.getStrings()
_base = DraftVecUtils.toString(self.node[0])
_cmd = 'Draft.makeWire'
_cmd += '('
_cmd += 'points, '
_cmd += 'placement=pl, '
_cmd += 'closed=' + str(closed) + ', '
_cmd += 'face=' + fil + ', '
_cmd += 'support=' + sup
_cmd += ')'
_cmd_list = ['pl = FreeCAD.Placement()',
'pl.Rotation.Q = ' + rot,
'pl.Base = ' + _base,
'points = ' + pts,
'line = ' + _cmd,
'Draft.autogroup(line)',
'FreeCAD.ActiveDocument.recompute()']
self.commit(translate("draft", "Create Wire"),
_cmd_list)
else:
import Draft
self.path = Draft.makeWire(self.node, closed=False, face=False)
# super(_CommandRoad, self).finish()
gui_base_original.Creator.finish(self)
if self.ui and self.ui.continueMode:
self.Activated()
self.makeRoad()
def makeRoad(self):
makeRoad(self.path)
def removeTemporaryObject(self):
"""Remove temporary object created."""
if self.obj:
try:
old = self.obj.Name
except ReferenceError:
# object already deleted, for some reason
pass
else:
todo.ToDo.delay(self.doc.removeObject, old)
self.obj = None
def undolast(self):
"""Undoes last line segment."""
import Part
if len(self.node) > 1:
self.node.pop()
# last = self.node[-1]
if self.obj.Shape.Edges:
edges = self.obj.Shape.Edges
if len(edges) > 1:
newshape = Part.makePolygon(self.node)
self.obj.Shape = newshape
else:
self.obj.ViewObject.hide()
# DNC: report on removal
# _msg(translate("draft", "Removing last point"))
_msg(translate("draft", "Pick next point"))
def drawSegment(self, point):
"""Draws new line segment."""
import Part
if self.planetrack and self.node:
self.planetrack.set(self.node[-1])
if len(self.node) == 1:
_msg(translate("draft", "Pick next point"))
elif len(self.node) == 2:
last = self.node[len(self.node) - 2]
newseg = Part.LineSegment(last, point).toShape()
self.obj.Shape = newseg
self.obj.ViewObject.Visibility = True
_msg(translate("draft", "Pick next point"))
if wire:
# Crear alignment desde el wire seleccionado
alignment = make_alignment_from_wire(wire)
road = makeRoad(alignment=alignment)
FreeCAD.Console.PrintMessage(
f"Carretera creada desde '{wire.Label}'. "
f"Alineamiento: {alignment.Label}\n")
else:
currentshape = self.obj.Shape.copy()
last = self.node[len(self.node) - 2]
if not DraftVecUtils.equals(last, point):
newseg = Part.LineSegment(last, point).toShape()
newshape = currentshape.fuse(newseg)
self.obj.Shape = newshape
_msg(translate("draft", "Pick next point"))
def wipe(self):
"""Remove all previous segments and starts from last point."""
if len(self.node) > 1:
# self.obj.Shape.nullify() # For some reason this fails
self.obj.ViewObject.Visibility = False
self.node = [self.node[-1]]
if self.planetrack:
self.planetrack.set(self.node[0])
_msg(translate("draft", "Pick next point"))
def orientWP(self):
"""Orient the working plane."""
import DraftGeomUtils
if hasattr(App, "DraftWorkingPlane"):
if len(self.node) > 1 and self.obj:
n = DraftGeomUtils.getNormal(self.obj.Shape)
if not n:
n = App.DraftWorkingPlane.axis
p = self.node[-1]
v = self.node[-2].sub(self.node[-1])
v = v.negative()
if not self.oldWP:
self.oldWP = App.DraftWorkingPlane.copy()
App.DraftWorkingPlane.alignToPointAndAxis(p, n, upvec=v)
if hasattr(Gui, "Snapper"):
Gui.Snapper.setGrid()
Gui.Snapper.restack()
if self.planetrack:
self.planetrack.set(self.node[-1])
def numericInput(self, numx, numy, numz):
"""Validate the entry fields in the user interface.
This function is called by the toolbar or taskpanel interface
when valid x, y, and z have been entered in the input fields.
"""
self.point = App.Vector(numx, numy, numz)
self.node.append(self.point)
self.drawSegment(self.point)
self.ui.setNextFocus()
FreeCAD.Console.PrintWarning(
"Selecciona un Wire (polilínea) para usarlo como eje de carretera.\n")
if FreeCAD.GuiUp:
+1162 -23
View File
File diff suppressed because it is too large Load Diff
+147 -71
View File
@@ -129,8 +129,14 @@ class Terrain(ArchComponent.Component):
# obj.IfcType = "Fence"
# obj.MoveWithHost = False
self.site = PVPlantSite.get()
self.site.Terrain = obj
try:
self.site = PVPlantSite.get()
except Exception:
self.site = None
if self.site:
self.site.Terrain = obj
else:
FreeCAD.Console.PrintWarning('Terrain: No se encontró Site, algunas funciones DEM requerirán Site.\n')
obj.ViewObject.ShapeColor = (0.0000, 0.6667, 0.4980)
obj.ViewObject.LineColor = (0.0000, 0.6000, 0.4392)
@@ -192,6 +198,12 @@ class Terrain(ArchComponent.Component):
if prop == "InitialMesh":
obj.mesh = obj.InitialMesh.copy()
# Forzar actualización visual
obj.publishProperty("Mesh")
if prop == "mesh":
# La propiedad mesh cambió → forzar recompute para que updateData se dispare
pass
if prop == "DEM" or prop == "CuttingBoundary":
from datetime import datetime
@@ -237,7 +249,7 @@ class Terrain(ArchComponent.Component):
del templist
# create xy coordinates
offset = self.site.Origin
offset = self.site.Origin if self.site else FreeCAD.Vector(0, 0, 0)
x = (cellsize * np.arange(nx)[0::coarse_factor] + xllvalue) * 1000 - offset.x
y = (cellsize * np.arange(ny)[-1::-1][0::coarse_factor] + yllvalue) * 1000 - offset.y
datavals = datavals * 1000 # Ajuste de altura
@@ -269,35 +281,95 @@ class Terrain(ArchComponent.Component):
stepx = math.ceil(nx / stepsize)
stepy = math.ceil(ny / stepsize)
# Malla completa primero como numpy y filtramos todo de una
from datetime import datetime
t_start = datetime.now()
# Crear grid completo de coordenadas
XX, YY = np.meshgrid(x, y)
ZZ = datavals.copy()
# Enmascarar nodata
mask_valida = ZZ != nodata_value
# Enmascarar cutting boundary si existe
if obj.CuttingBoundary:
from FreeCAD import Base
shape = obj.CuttingBoundary.Shape
mask_boundary = np.zeros_like(ZZ, dtype=bool)
# Sampling: revisar solo puntos estratégicos para boundary grande
stride = max(1, min(nx, ny) // 200)
for i in range(0, ny, stride):
for j in range(0, nx, stride):
if mask_valida[i, j]:
if shape.isInside(FreeCAD.Vector(x[j], y[i], 0), 0, True):
mask_boundary[i, j] = True
mask_valida = mask_valida & mask_boundary
# Extraer puntos válidos como lista plana
pts_validos = np.column_stack([
XX[mask_valida].ravel(),
YY[mask_valida].ravel(),
ZZ[mask_valida].ravel()
])
del XX, YY, ZZ, mask_valida
# Triangulación completa de una vez (no por parches)
mesh = Mesh.Mesh()
for indx in range(stepx):
inix = indx * stepsize - 1
finx = min([stepsize * (indx + 1), len(x)-1])
for indy in range(stepy):
iniy = indy * stepsize - 1
finy = min([stepsize * (indy + 1), len(y) - 1])
pts = []
for i in range(inix, finx):
for j in range(iniy, finy):
if datavals[j][i] != nodata_value:
if obj.CuttingBoundary:
if obj.CuttingBoundary.Shape.isInside(FreeCAD.Vector(x[i], y[j], 0), 0, True):
pts.append([x[i], y[j], datavals[j][i]])
else:
pts.append([x[i], y[j], datavals[j][i]])
if len(pts) > 3:
if len(pts_validos) > 3:
# Si hay muchos puntos, triangulamos por parches para evitar OOM
patch_size = 50000
n_patches = max(1, math.ceil(len(pts_validos) / patch_size))
for p in range(n_patches):
patch = pts_validos[p * patch_size:(p + 1) * patch_size].tolist()
if len(patch) > 3:
try:
triangulated = Triangulation.Triangulate(pts)
triangulated = Triangulation.Triangulate(patch)
mesh.addMesh(triangulated)
except TypeError:
print(f"Error al procesar {len(pts)} puntos: {str(e)}")
except TypeError as e:
print(f"Patch {p}: error al procesar {len(patch)} puntos: {str(e)}")
except Exception as e:
print(f"Patch {p}: error inesperado: {str(e)}")
print(f'Terraín DEM: {len(pts_validos)} pts válidos, {n_patches} parches, {datetime.now()-t_start}')
del pts_validos
mesh.removeDuplicatedPoints()
mesh.removeFoldsOnSurface()
obj.InitialMesh = mesh.copy()
Mesh.show(mesh)
# Limpiar objetos mesh huérfanos previos si existen
for o in FreeCAD.ActiveDocument.Objects:
if o.TypeId == 'Mesh::Feature' and o.Label.startswith('Terrain_mesh_'):
FreeCAD.ActiveDocument.removeObject(o.Name)
mesh_obj = Mesh.show(mesh)
mesh_obj.Label = 'Terrain_mesh_' + obj.Label
elif suffix in ['.xyz']:
data = open_xyz_mmap(obj.DEM)
pts_array = open_xyz_mmap(obj.DEM)
if pts_array is not None and len(pts_array) > 3:
import MeshTools.Triangulation as Triangulation
import Mesh
if obj.CuttingBoundary:
mask = []
for pt in pts_array:
mask.append(obj.CuttingBoundary.Shape.isInside(
FreeCAD.Vector(pt[0], pt[1], 0), 0, True))
pts_array = pts_array[mask]
if len(pts_array) > 3:
from datetime import datetime
t0 = datetime.now()
pts_list = pts_array.tolist()
mesh = Triangulation.Triangulate(pts_list)
mesh.removeDuplicatedPoints()
mesh.removeFoldsOnSurface()
obj.InitialMesh = mesh.copy()
# Limpiar objetos mesh huérfanos previos
for o in FreeCAD.ActiveDocument.Objects:
if o.TypeId == 'Mesh::Feature' and o.Label.startswith('Terrain_mesh_'):
FreeCAD.ActiveDocument.removeObject(o.Name)
mesh_obj = Mesh.show(mesh)
mesh_obj.Label = 'Terrain_mesh_' + obj.Label
print(f'XYZ import: {len(pts_array)} puntos en {datetime.now()-t0}')
@@ -329,6 +401,11 @@ class Terrain(ArchComponent.Component):
if obj.DEM:
obj.DEM = None
obj.mesh = mesh
# Forzar actualización visual llamando a publishProperty
try:
obj.publishProperty("Mesh")
except:
pass
def execute(self, obj):
''''''
@@ -547,47 +624,47 @@ class ViewProviderTerrain:
offset.factor = -2.0
# Boundary features.
'''self.boundary_color = coin.SoBaseColor()
self.boundary_color = coin.SoBaseColor()
self.boundary_coords = coin.SoGeoCoordinate()
self.boundary_lines = coin.SoLineSet()
self.boundary_style = coin.SoDrawStyle()
self.boundary_style.style = coin.SoDrawStyle.LINES'''
self.boundary_style.style = coin.SoDrawStyle.LINES
# Boundary root.
'''boundaries = coin.SoType.fromName('SoFCSelection').createInstance()
boundaries = coin.SoType.fromName('SoFCSelection').createInstance()
boundaries.style = 'EMISSIVE_DIFFUSE'
boundaries.addChild(self.boundary_color)
boundaries.addChild(self.boundary_style)
boundaries.addChild(self.boundary_coords)
boundaries.addChild(self.boundary_lines)'''
boundaries.addChild(self.boundary_lines)
# Major Contour features.
'''self.major_color = coin.SoBaseColor()
self.major_color = coin.SoBaseColor()
self.major_coords = coin.SoGeoCoordinate()
self.major_lines = coin.SoLineSet()
self.major_style = coin.SoDrawStyle()
self.major_style.style = coin.SoDrawStyle.LINES'''
self.major_style.style = coin.SoDrawStyle.LINES
# Major Contour root.
'''major_contours = coin.SoSeparator()
major_contours = coin.SoSeparator()
major_contours.addChild(self.major_color)
major_contours.addChild(self.major_style)
major_contours.addChild(self.major_coords)
major_contours.addChild(self.major_lines)'''
major_contours.addChild(self.major_lines)
# Minor Contour features.
'''self.minor_color = coin.SoBaseColor()
self.minor_color = coin.SoBaseColor()
self.minor_coords = coin.SoGeoCoordinate()
self.minor_lines = coin.SoLineSet()
self.minor_style = coin.SoDrawStyle()
self.minor_style.style = coin.SoDrawStyle.LINES'''
self.minor_style.style = coin.SoDrawStyle.LINES
# Minor Contour root.
'''minor_contours = coin.SoSeparator()
minor_contours = coin.SoSeparator()
minor_contours.addChild(self.minor_color)
minor_contours.addChild(self.minor_style)
minor_contours.addChild(self.minor_coords)
minor_contours.addChild(self.minor_lines)'''
minor_contours.addChild(self.minor_lines)
# Highlight for selection.
highlight = coin.SoType.fromName('SoFCSelection').createInstance()
@@ -596,7 +673,7 @@ class ViewProviderTerrain:
highlight.addChild(mat_binding)
highlight.addChild(self.geo_coords)
highlight.addChild(self.triangles)
#highlight.addChild(boundaries)
highlight.addChild(boundaries)
# Face root.
face = coin.SoSeparator()
@@ -609,19 +686,19 @@ class ViewProviderTerrain:
edge.addChild(self.edge_style)
edge.addChild(highlight)
# Surface root.
# Surface root - con contour lines visibles.
surface_root = coin.SoSeparator()
surface_root.addChild(face)
surface_root.addChild(offset)
surface_root.addChild(edge)
#surface_root.addChild(major_contours)
#surface_root.addChild(minor_contours)
surface_root.addChild(major_contours)
surface_root.addChild(minor_contours)
vobj.addDisplayMode(surface_root, "Surface")
# Boundary root.
#boundary_root = coin.SoSeparator()
#boundary_root.addChild(boundaries)
#vobj.addDisplayMode(boundary_root, "Boundary")
boundary_root = coin.SoSeparator()
boundary_root.addChild(boundaries)
vobj.addDisplayMode(boundary_root, "Boundary")
# Elevation/Shaded root.
'''shaded_root = coin.SoSeparator()
@@ -648,52 +725,50 @@ class ViewProviderTerrain:
self.onChanged(vobj, "ShapeColor")
self.onChanged(vobj, "LineColor")
self.onChanged(vobj, "LineWidth")
#self.onChanged(vobj, "BoundaryColor")
#self.onChanged(vobj, "BoundaryWidth")
#self.onChanged(vobj, "BoundaryPattern")
#self.onChanged(vobj, "PatternScale")
#self.onChanged(vobj, "MajorColor")
#self.onChanged(vobj, "MajorWidth")
#self.onChanged(vobj, "MinorColor")
#self.onChanged(vobj, "MinorWidth")
self.onChanged(vobj, "BoundaryColor")
self.onChanged(vobj, "BoundaryWidth")
self.onChanged(vobj, "BoundaryPattern")
self.onChanged(vobj, "PatternScale")
self.onChanged(vobj, "MajorColor")
self.onChanged(vobj, "MajorWidth")
self.onChanged(vobj, "MinorColor")
self.onChanged(vobj, "MinorWidth")
def updateData(self, obj, prop):
''' Update Object visuals when a data property changed. '''
# Set geosystem.
geo_system = ["UTM", FreeCAD.ActiveDocument.Site.UtmZone, "FLAT"]
try:
utm_zone = FreeCAD.ActiveDocument.Site.UtmZone
except:
utm_zone = "30"
geo_system = ["UTM", utm_zone, "FLAT"]
self.geo_coords.geoSystem.setValues(geo_system)
'''
self.boundary_coords.geoSystem.setValues(geo_system)
self.major_coords.geoSystem.setValues(geo_system)
self.minor_coords.geoSystem.setValues(geo_system)
'''
if prop == "Mesh":
if prop == "mesh" or prop == "Mesh":
if obj.mesh:
print("Mostrar mesh")
mesh = obj.mesh
vertices = [tuple(v) for v in mesh.Topology[0]]
faces = []
for face in mesh.Topology[1]:
faces.extend(face)
faces.append(-1)
try:
vertices = [tuple(v) for v in mesh.Topology[0]]
faces = []
for face in mesh.Topology[1]:
faces.extend(face)
faces.append(-1)
# Asignar a los nodos de visualización
self.geo_coords.point.values = vertices # <-- ¡Clave!
self.triangles.coordIndex.values = faces # <-- ¡Clave!
# Asignar a los nodos de visualización
self.geo_coords.point.values = vertices
self.triangles.coordIndex.values = faces
except Exception as e:
FreeCAD.Console.PrintError(f"Error actualizando mesh visual: {e}\n")
def getDisplayModes(self, vobj):
''' Return a list of display modes. '''
modes = ["Surface", "Boundary"]
return modes
return ["Surface", "Boundary", "Flat Lines", "Wireframe"]
def getDefaultDisplayMode(self):
'''
Return the name of the default display mode.
'''
return "Surface"
def claimChildren(self):
@@ -736,3 +811,4 @@ class ViewProviderTerrain:
if FreeCAD.GuiUp:
FreeCADGui.addCommand('Terrain', _CommandTerrain())'''
+9 -29
View File
@@ -450,35 +450,18 @@ class ContourTaskPanel():
starttime = datetime.now()
if self.land is None:
print("No hay objetos para procesar")
FreeCAD.Console.PrintWarning("No hay objetos para procesar\n")
return False
else:
minor = FreeCAD.Units.Quantity(self.inputMinorContourMargin.currentText()).Value
mayor = FreeCAD.Units.Quantity(self.inputMayorContourMargin.currentText()).Value
i = 2
if i == 0:
makeContours(self.land, minor, mayor, self.MinorColor, self.MayorColor,
self.inputMinorContourThickness.value(), self.inputMayorContourThickness.value())
elif i == 1:
import multiprocessing
p = multiprocessing.Process(target=makeContours,
args=(self.land, minor, mayor,
self.MinorColor, self.MayorColor,
self.inputMinorContourThickness.value(),
self.inputMayorContourThickness.value(), ))
p.start()
p.join()
else:
import threading
hilo = threading.Thread(target = makeContours,
args = (self.land, minor, mayor,
self.MinorColor, self.MayorColor,
self.inputMinorContourThickness.value(),
self.inputMayorContourThickness.value()))
hilo.daemon = True
hilo.start()
makeContours(
self.land, minor, mayor,
self.MinorColor, self.MayorColor,
self.inputMinorContourThickness.value(),
self.inputMayorContourThickness.value()
)
total_time = datetime.now() - starttime
print(" -- Tiempo tardado:", total_time)
@@ -569,7 +552,7 @@ class SlopeTaskPanel(_generalTaskPanel):
land.ViewObject.DiffuseColor = colorlist
# TODO: check this code:
elif obj.isDerivedFrom("Mesh::Feature"):
elif hasattr(land, 'Mesh') and land.isDerivedFrom("Mesh::Feature"):
fMesh = Mest2FemMesh(land)
import math
setColors = []
@@ -602,10 +585,7 @@ class SlopeTaskPanel(_generalTaskPanel):
print("Everything OK (", datetime.now() - starttime, ")")
def accept(self):
# self.getPointSlope()
import threading
hilo = threading.Thread(target=self.getPointSlope(self.ranges))
hilo.start()
self.getPointSlope(self.ranges)
return True
# Orientation Analisys: ---------------------------------------------------------------------------------
+1 -1
View File
@@ -1,6 +1,6 @@
import FreeCAD
import FreeCADGui
from PySide2 import QtWidgets
from PySide import QtWidgets
import os
if FreeCAD.GuiUp:
-43
View File
@@ -1,43 +0,0 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 19.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
<polygon style="fill:#FFB74F;" points="432.106,250.534 432.106,470.021 296.578,470.021 296.578,336.975 221.399,336.975
221.399,470.021 79.894,470.021 79.894,250.534 256,115.075 "/>
<path style="fill:#FF7D3C;" d="M439.485,183.135V90.306h-74.167v35.772L256,41.979L0,238.92l53.633,69.712L256,152.959
l202.367,155.672L512,238.92L439.485,183.135z"/>
<polygon style="fill:#FF9A00;" points="432.106,250.534 432.106,470.021 296.578,470.021 296.578,336.975 256,336.975 256,115.075
"/>
<polygon style="fill:#FF4E19;" points="512,238.92 458.367,308.632 256,152.959 256,41.979 365.318,126.078 365.318,90.306
439.485,90.306 439.485,183.135 "/>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
<g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

-45
View File
@@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
id="Layer_1"
enable-background="new 0 0 511.771 511.771"
height="512"
viewBox="0 0 511.771 511.771"
width="512"
version="1.1"
sodipodi:docname="stringsetup.svg"
inkscape:version="1.1 (c68e22c387, 2021-05-23)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
>
<defs
id="defs11" />
<sodipodi:namedview
id="namedview9"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
showgrid="false"
inkscape:zoom="1.4707031"
inkscape:cx="256"
inkscape:cy="256"
inkscape:window-width="2160"
inkscape:window-height="1361"
inkscape:window-x="-9"
inkscape:window-y="-9"
inkscape:window-maximized="1"
inkscape:current-layer="Layer_1" />
<g
id="g6"
transform="matrix(0.99955273,0,0,0.99955273,-8.003632e-5,1.12e-6)">
<g
id="g4">
<path
d="m 496.659,312.107 -47.061,-36.8 c 0.597,-5.675 1.109,-12.309 1.109,-19.328 0,-7.019 -0.491,-13.653 -1.109,-19.328 l 47.104,-36.821 c 8.747,-6.912 11.136,-19.179 5.568,-29.397 L 453.331,85.76 C 448.104,76.203 436.648,71.296 425.022,75.584 L 369.491,97.877 C 358.846,90.197 347.688,83.712 336.147,78.528 L 327.699,19.627 C 326.312,8.448 316.584,0 305.086,0 h -98.133 c -11.499,0 -21.205,8.448 -22.571,19.456 l -8.469,59.115 c -11.179,5.035 -22.165,11.435 -33.28,19.349 L 86.953,75.563 C 76.52,71.531 64.04,76.053 58.856,85.568 L 9.854,170.347 c -5.781,9.771 -3.392,22.464 5.547,29.547 l 47.061,36.8 c -0.747,7.189 -1.109,13.44 -1.109,19.307 0,5.867 0.363,12.117 1.109,19.328 L 15.358,312.15 c -8.747,6.933 -11.115,19.2 -5.547,29.397 l 48.939,84.672 c 5.227,9.536 16.576,14.485 28.309,10.176 l 55.531,-22.293 c 10.624,7.659 21.781,14.144 33.323,19.349 l 8.448,58.88 C 185.747,503.552 195.454,512 206.974,512 h 98.133 c 11.499,0 21.227,-8.448 22.592,-19.456 l 8.469,-59.093 c 11.179,-5.056 22.144,-11.435 33.28,-19.371 l 55.68,22.357 c 2.688,1.045 5.483,1.579 8.363,1.579 8.277,0 15.893,-4.523 19.733,-11.563 l 49.152,-85.12 c 5.462,-9.984 3.072,-22.25 -5.717,-29.226 z m -240.64,29.226 c -47.061,0 -85.333,-38.272 -85.333,-85.333 0,-47.061 38.272,-85.333 85.333,-85.333 47.061,0 85.333,38.272 85.333,85.333 0,47.061 -38.272,85.333 -85.333,85.333 z"
id="path2" />
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 2.5 KiB

-132
View File
@@ -1,132 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
inkscape:version="1.0 (4035a4fb49, 2020-05-01)"
sodipodi:docname="trench.svg"
id="svg66"
version="1.1"
width="512pt"
viewBox="0 0 512 512"
height="512pt">
<metadata
id="metadata72">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs70" />
<sodipodi:namedview
inkscape:current-layer="svg66"
inkscape:window-maximized="1"
inkscape:window-y="-9"
inkscape:window-x="-9"
inkscape:cy="341.33333"
inkscape:cx="341.33333"
inkscape:zoom="1.0766602"
showgrid="false"
id="namedview68"
inkscape:window-height="1361"
inkscape:window-width="2160"
inkscape:pageshadow="2"
inkscape:pageopacity="0"
guidetolerance="10"
gridtolerance="10"
objecttolerance="10"
borderopacity="1"
bordercolor="#666666"
pagecolor="#ffffff" />
<path
id="path2"
fill="#ffb655"
d="m359.78125 71.285156v288.496094h-207.5625v-288.496094h-144.71875v433.214844h497v-433.214844zm0 0" />
<path
id="path4"
fill="#a4e276"
d="m7.5 15.5c18.089844 0 18.089844-8 36.183594-8 18.089844 0 18.089844 8 36.179687 8 18.089844 0 18.089844-8 36.175781-8 18.089844 0 18.089844 8 36.179688 8v55.785156h-144.71875zm0 0" />
<path
id="path6"
fill="#a4e276"
d="m359.78125 15.5c18.089844 0 18.089844-8 36.183594-8 18.089844 0 18.089844 8 36.179687 8 18.089844 0 18.089844-8 36.175781-8 18.089844 0 18.089844 8 36.179688 8v55.785156h-144.71875zm0 0" />
<path
id="path16"
fill="#ff7956"
d="m359.78125 71.285156h30v288.496094h-30zm0 0" />
<path
id="path18"
fill="#ff7956"
d="m7.5 71.285156h30v433.214844h-30zm0 0" />
<path
id="path20"
fill="#64c37d"
d="m58.683594 10.179688c-3.6875-1.476563-7.984375-2.679688-15-2.679688-18.09375 0-18.09375 8-36.183594 8v55.785156h30v-55.785156c11.070312 0 15.367188-2.996094 21.183594-5.320312zm0 0" />
<path
id="path22"
fill="#64c37d"
d="m410.964844 10.179688c-3.6875-1.476563-7.984375-2.679688-15-2.679688-18.09375 0-18.09375 8-36.183594 8v55.785156h30v-55.785156c11.070312 0 15.367188-2.996094 21.183594-5.320312zm0 0" />
<path
id="path28"
d="m144.71875 91.289062v275.992188h67.402344v-15h-52.402344v-260.992188zm0 0" />
<path
id="path30"
d="m299.871094 367.28125h67.410156v-275.992188h-15v260.992188h-52.410156zm0 0" />
<path
id="path32"
d="m497 497h-482v-405.714844h-15v420.714844h512v-420.714844h-15zm0 0" />
<path
id="path34"
d="m159.71875 8h-7.5c-7.460938 0-10.8125-1.480469-15.054688-3.359375-4.917968-2.175781-10.492187-4.640625-21.125-4.640625-10.628906 0-16.203124 2.464844-21.121093 4.640625-4.242188 1.878906-7.59375 3.359375-15.054688 3.359375-7.460937 0-10.8125-1.480469-15.058593-3.359375-4.917969-2.175781-10.492188-4.640625-21.121094-4.640625-10.632813 0-16.207032 2.464844-21.125 4.640625-4.246094 1.878906-7.597656 3.359375-15.058594 3.359375h-7.5v70.785156h159.71875zm-15 55.785156h-129.71875v-41.257812c6.054688-.820313 10.015625-2.574219 13.625-4.167969 4.242188-1.878906 7.597656-3.359375 15.058594-3.359375 7.460937 0 10.8125 1.480469 15.054687 3.359375 4.917969 2.175781 10.496094 4.640625 21.125 4.640625 10.628907 0 16.203125-2.464844 21.121094-4.640625 4.246094-1.878906 7.597656-3.359375 15.054687-3.359375 7.460938 0 10.8125 1.480469 15.058594 3.359375 3.605469 1.59375 7.570313 3.347656 13.621094 4.167969zm0 0" />
<path
id="path36"
d="m504.5 8c-7.460938 0-10.8125-1.480469-15.054688-3.359375-4.917968-2.175781-10.496093-4.640625-21.125-4.640625-10.628906 0-16.203124 2.464844-21.121093 4.640625-4.242188 1.878906-7.59375 3.359375-15.054688 3.359375-7.460937 0-10.8125-1.480469-15.058593-3.359375-4.917969-2.175781-10.492188-4.640625-21.121094-4.640625-10.632813 0-16.207032 2.464844-21.125 4.640625-4.246094 1.878906-7.597656 3.359375-15.058594 3.359375h-7.5v70.785156h159.71875v-70.785156zm-7.5 55.785156h-129.71875v-41.257812c6.054688-.820313 10.015625-2.574219 13.625-4.167969 4.242188-1.878906 7.597656-3.359375 15.058594-3.359375 7.460937 0 10.8125 1.480469 15.054687 3.359375 4.917969 2.175781 10.496094 4.640625 21.125 4.640625 10.628907 0 16.203125-2.464844 21.121094-4.640625 4.246094-1.878906 7.597656-3.359375 15.058594-3.359375 7.457031 0 10.8125 1.480469 15.054687 3.359375 3.609375 1.59375 7.570313 3.347656 13.621094 4.167969zm0 0" />
<path
id="path40"
d="m131.007812 423.90625h15v15h-15zm0 0" />
<path
id="path42"
d="m348.007812 456.929688h15v15h-15zm0 0" />
<path
id="path44"
d="m386.429688 430.050781h15v15h-15zm0 0" />
<path
id="path46"
d="m423.140625 260.738281h15v15h-15zm0 0" />
<path
id="path48"
d="m84.722656 403.050781h15v15h-15zm0 0" />
<path
id="path50"
d="m384.929688 294.355469h15v15h-15zm0 0" />
<path
id="path52"
d="m50.222656 130.597656h15v15h-15zm0 0" />
<path
id="path54"
d="m92.222656 98.785156h15v15h-15zm0 0" />
<path
id="path56"
d="m430.640625 313.023438h15v15h-15zm0 0" />
<path
id="path58"
d="m57.722656 439.050781h15v15h-15zm0 0" />
<g
id="g64"
fill="#fff">
<path
id="path60"
d="m487 318.523438h-15v-172.238282h15zm0-182.238282h-15v-15h15zm0-25h-15v-15h15zm0 0" />
<path
id="path62"
d="m486.5 53.785156h-25.71875v-15h25.71875zm-35.71875 0h-15v-15h15zm-25 0h-15v-15h15zm0 0" />
</g>
</svg>

Before

Width:  |  Height:  |  Size: 5.9 KiB

+233
View File
@@ -0,0 +1,233 @@
# /**********************************************************************
# * *
# * Copyright (c) 2026 Javier Braña <javier.branagutierrez@gmail.com> *
# * *
# * Alignment - Alineamiento horizontal y vertical *
# * *
# * Define el eje de la carretera mediante: *
# * - Alineamiento horizontal: polilínea + curvas circulares *
# * - Alineamiento vertical: rasante con pendientes y curvas verticales*
# * - Estaciones (progresivas) *
# * *
# ***********************************************************************
import FreeCAD
import Part
import math
import numpy as np
def make_alignment_from_wire(wire, name="Alignment"):
"""Crea un objeto Alignment a partir de un wire de FreeCAD."""
if FreeCAD.ActiveDocument is None:
return None
obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", name)
Alignment(obj)
_ViewProviderAlignment(obj.ViewObject)
obj.SourceWire = wire
obj.Label = name
FreeCAD.ActiveDocument.recompute()
return obj
class Alignment:
"""
Alineamiento horizontal + vertical de una carretera.
Propiedades principales:
SourceWire : Polilínea base del eje
Stations : Progresivas (lista de distancias)
StationInterval : Intervalo entre estaciones
TotalLength : Longitud total del eje
HorizontalCurves : Curvas circulares (radio, longitud, parámetros)
VerticalPVI : Puntos de intersección vertical (PVI) para la rasante
"""
def __init__(self, obj):
obj.Proxy = self
self.setProperties(obj)
self._cached_chainage = None
self._cached_station_points = None
self._cached_tangents = None
def setProperties(self, obj):
pl = obj.PropertiesList
if "SourceWire" not in pl:
obj.addProperty("App::PropertyLink",
"SourceWire", "Alignment",
"Polilínea base del eje")
if "Stations" not in pl:
obj.addProperty("App::PropertyFloatList",
"Stations", "Alignment",
"Estaciones (progresivas) en mm")
if "StationInterval" not in pl:
obj.addProperty("App::PropertyLength",
"StationInterval", "Alignment",
"Intervalo entre estaciones").StationInterval = 20000
if "NumberOfStations" not in pl:
obj.addProperty("App::PropertyInteger",
"NumberOfStations", "Alignment",
"Número de estaciones")
obj.setEditorMode("NumberOfStations", 1)
if "TotalLength" not in pl:
obj.addProperty("App::PropertyLength",
"TotalLength", "Alignment",
"Longitud total del eje")
obj.setEditorMode("TotalLength", 1)
if "HorizontalCurveRadii" not in pl:
obj.addProperty("App::PropertyFloatList",
"HorizontalCurveRadii", "Alignment",
"Radios de curva en cada vértice (0 = recta)")
if "ShowStations" not in pl:
obj.addProperty("App::PropertyBool",
"ShowStations", "Alignment",
"Mostrar marcas de estación en 3D").ShowStations = False
def onDocumentRestored(self, obj):
self.setProperties(obj)
def execute(self, obj):
"""Calcula estaciones y geometría del alignment."""
if not obj.SourceWire or not obj.SourceWire.Shape:
return
wire = obj.SourceWire.Shape
if wire.isNull():
return
total_len = wire.Length
obj.TotalLength = total_len
if total_len <= 0:
return
interval = obj.StationInterval.Value
if interval <= 0:
interval = 20000
n_stations = max(2, int(total_len / interval) + 1)
obj.NumberOfStations = n_stations
stations = np.linspace(0, total_len, n_stations).tolist()
obj.Stations = stations
# Calcular radios de curva en cada vértice del wire
self._compute_curve_radii(obj, wire)
# Cache
self._cached_chainage = stations
self._cached_station_points = None
self._cached_tangents = None
def _compute_curve_radii(self, obj, wire):
"""Calcula el radio de curvatura en cada vértice de la polilínea."""
vertices = wire.Vertexes
n = len(vertices)
if n < 3:
obj.HorizontalCurveRadii = []
return
radii = []
for i in range(1, n - 1):
p0 = vertices[i - 1].Point
p1 = vertices[i].Point
p2 = vertices[i + 1].Point
v1 = p1 - p0
v2 = p2 - p1
cross = FreeCAD.Vector(0, 0, 1).dot(v1.cross(v2))
if abs(cross) < 1.0: # casi colineal
radii.append(0.0)
else:
# Radio aproximado = |v1| * |v2| / |v1 x v2|
r = v1.Length * v2.Length / abs(cross)
radii.append(round(r, 0))
obj.HorizontalCurveRadii = radii
def get_station_point(self, obj, distance):
"""Devuelve el punto 3D en el eje a una progresiva dada (mm)."""
if not obj.SourceWire:
return None
try:
wire = obj.SourceWire.Shape
param = wire.getParameterByLength(distance / obj.TotalLength)
return wire.valueAt(param)
except Exception:
return None
def get_tangent_at(self, obj, distance):
"""Devuelve el vector tangente en una progresiva (normalizado)."""
if not obj.SourceWire:
return None
try:
wire = obj.SourceWire.Shape
param = wire.getParameterByLength(distance / obj.TotalLength)
t = wire.tangentAt(param)
if t.Length > 0:
t.normalize()
return t
except Exception:
return None
def get_perpendicular_at(self, obj, distance):
"""Devuelve el vector perpendicular (horizontal) en una progresiva."""
t = self.get_tangent_at(obj, distance)
if t is None:
return None
perp = FreeCAD.Vector(-t.y, t.x, 0)
perp.normalize()
return perp
def get_station_data(self, obj):
"""
Devuelve arrays de (puntos, tangentes, perpendiculares) para todas las estaciones.
Returns:
tuple: (points, tangents, perps) listas de FreeCAD.Vector
"""
stations = obj.Stations
if not stations:
return [], [], []
points = []
tangents = []
perps = []
for s in stations:
pt = self.get_station_point(obj, s)
tg = self.get_tangent_at(obj, s)
pp = self.get_perpendicular_at(obj, s)
if pt and tg and pp:
points.append(pt)
tangents.append(tg)
perps.append(pp)
return points, tangents, perps
def __getstate__(self):
return None
def __setstate__(self, state):
return None
class _ViewProviderAlignment:
def __init__(self, vobj):
vobj.Proxy = self
def getIcon(self):
return ""
def __getstate__(self):
return None
def __setstate__(self, state):
return None
+7
View File
@@ -0,0 +1,7 @@
# Road Module - PVPlant Workbench
# Sistema de carreteras con alineamiento profesional
from .Alignment import make_alignment_from_wire, Alignment
from .CrossSection import CrossSectionBuilder
from .Road import make_road, Road
from .CutFill import calculate_cut_fill
+1 -1
View File
@@ -61,7 +61,7 @@ class SelObserver:
def onAceptClick(self):
''' '''
from PVPlantPlacement import moveFrameHead
from Civil.PVPlantPlacementCalc import moveFrameHead
moveFrameHead(self.obj, head=self.ui.comboHead.currentIndex(),
dist=self.ui.editDist.value())
self.setUI(self.obj)
-348
View File
@@ -1,348 +0,0 @@
# -*- coding: utf-8 -*-
__title__ = "Freehand BSpline"
__author__ = "Christophe Grellier (Chris_G)"
__license__ = "LGPL 2.1"
__doc__ = "Creates an freehand BSpline curve"
__usage__ = """*** Interpolation curve control keys :
a - Select all / Deselect
i - Insert point in selected segments
t - Set / unset tangent (view direction)
p - Align selected objects
s - Snap points on shape / Unsnap
l - Set/unset a linear interpolation
x,y,z - Axis constraints during grab
q - Apply changes and quit editing"""
import os
import FreeCAD
import FreeCADGui
import Part
from . import ICONPATH
from . import _utils
from . import profile_editor
TOOL_ICON = os.path.join(ICONPATH, 'editableSpline.svg')
# debug = _utils.debug
debug = _utils.doNothing
def check_pivy():
try:
profile_editor.MarkerOnShape([FreeCAD.Vector()])
return True
except Exception as exc:
FreeCAD.Console.PrintWarning(str(exc) + "\nPivy interaction library failure\n")
return False
def midpoint(e):
p = e.FirstParameter + 0.5 * (e.LastParameter - e.FirstParameter)
return e.valueAt(p)
class GordonProfileFP:
"""Creates an editable interpolation curve"""
def __init__(self, obj, s, d, t):
"""Add the properties"""
obj.addProperty("App::PropertyLinkSubList", "Support", "Profile", "Constraint shapes").Support = s
obj.addProperty("App::PropertyFloatConstraint", "Parametrization", "Profile", "Parametrization factor")
obj.addProperty("App::PropertyFloat", "Tolerance", "Profile", "Tolerance").Tolerance = 1e-5
obj.addProperty("App::PropertyBool", "Periodic", "Profile", "Periodic curve").Periodic = False
obj.addProperty("App::PropertyVectorList", "Data", "Profile", "Data list").Data = d
obj.addProperty("App::PropertyVectorList", "Tangents", "Profile", "Tangents list")
obj.addProperty("App::PropertyBoolList", "Flags", "Profile", "Tangent flags")
obj.addProperty("App::PropertyIntegerList", "DataType", "Profile", "Types of interpolated points").DataType = t
obj.addProperty("App::PropertyBoolList", "LinearSegments", "Profile", "Linear segment flags")
obj.Parametrization = (1.0, 0.0, 1.0, 0.05)
obj.Proxy = self
def get_shapes(self, fp):
if hasattr(fp, 'Support'):
sl = list()
for ob, names in fp.Support:
for name in names:
if "Vertex" in name:
n = eval(name.lstrip("Vertex"))
if len(ob.Shape.Vertexes) >= n:
sl.append(ob.Shape.Vertexes[n - 1])
elif ("Point" in name):
sl.append(Part.Vertex(ob.Shape.Point))
elif ("Edge" in name):
n = eval(name.lstrip("Edge"))
if len(ob.Shape.Edges) >= n:
sl.append(ob.Shape.Edges[n - 1])
elif ("Face" in name):
n = eval(name.lstrip("Face"))
if len(ob.Shape.Faces) >= n:
sl.append(ob.Shape.Faces[n - 1])
return sl
def get_points(self, fp, stretch=True):
touched = False
shapes = self.get_shapes(fp)
if not len(fp.Data) == len(fp.DataType):
FreeCAD.Console.PrintError("Gordon Profile : Data and DataType mismatch\n")
return(None)
pts = list()
shape_idx = 0
for i in range(len(fp.Data)):
if fp.DataType[i] == 0: # Free point
pts.append(fp.Data[i])
elif (fp.DataType[i] == 1):
if (shape_idx < len(shapes)): # project on shape
d, p, i = Part.Vertex(fp.Data[i]).distToShape(shapes[shape_idx])
if d > fp.Tolerance:
touched = True
pts.append(p[0][1]) # shapes[shape_idx].valueAt(fp.Data[i].x))
shape_idx += 1
else:
pts.append(fp.Data[i])
if stretch and touched:
params = [0]
knots = [0]
moves = [pts[0] - fp.Data[0]]
lsum = 0
mults = [2]
for i in range(1, len(pts)):
lsum += fp.Data[i - 1].distanceToPoint(fp.Data[i])
params.append(lsum)
if fp.DataType[i] == 1:
knots.append(lsum)
moves.append(pts[i] - fp.Data[i])
mults.insert(1, 1)
mults[-1] = 2
if len(moves) < 2:
return(pts)
# FreeCAD.Console.PrintMessage("%s\n%s\n%s\n"%(moves,mults,knots))
curve = Part.BSplineCurve()
curve.buildFromPolesMultsKnots(moves, mults, knots, False, 1)
for i in range(1, len(pts)):
if fp.DataType[i] == 0:
# FreeCAD.Console.PrintMessage("Stretch %s #%d: %s to %s\n"%(fp.Label,i,pts[i],curve.value(params[i])))
pts[i] += curve.value(params[i])
if touched:
return pts
else:
return False
def execute(self, obj):
try:
o = FreeCADGui.ActiveDocument.getInEdit().Object
if o == obj:
return
except:
FreeCAD.Console.PrintWarning("execute is disabled during editing\n")
pts = self.get_points(obj)
if pts:
if len(pts) < 2:
FreeCAD.Console.PrintError("{} : Not enough points\n".format(obj.Label))
return False
else:
obj.Data = pts
else:
pts = obj.Data
tans = [FreeCAD.Vector()] * len(pts)
flags = [False] * len(pts)
for i in range(len(obj.Tangents)):
tans[i] = obj.Tangents[i]
for i in range(len(obj.Flags)):
flags[i] = obj.Flags[i]
# if not (len(obj.LinearSegments) == len(pts)-1):
# FreeCAD.Console.PrintError("%s : Points and LinearSegments mismatch\n"%obj.Label)
if len(obj.LinearSegments) > 0:
for i, b in enumerate(obj.LinearSegments):
if b:
tans[i] = pts[i + 1] - pts[i]
tans[i + 1] = tans[i]
flags[i] = True
flags[i + 1] = True
params = profile_editor.parameterization(pts, obj.Parametrization, obj.Periodic)
curve = Part.BSplineCurve()
if len(pts) == 2:
curve.buildFromPoles(pts)
elif obj.Periodic and pts[0].distanceToPoint(pts[-1]) < 1e-7:
curve.interpolate(Points=pts[:-1], Parameters=params, PeriodicFlag=obj.Periodic, Tolerance=obj.Tolerance, Tangents=tans[:-1], TangentFlags=flags[:-1])
else:
curve.interpolate(Points=pts, Parameters=params, PeriodicFlag=obj.Periodic, Tolerance=obj.Tolerance, Tangents=tans, TangentFlags=flags)
obj.Shape = curve.toShape()
def onChanged(self, fp, prop):
if prop in ("Support", "Data", "DataType", "Periodic"):
# FreeCAD.Console.PrintMessage("%s : %s changed\n"%(fp.Label,prop))
if (len(fp.Data) == len(fp.DataType)) and (sum(fp.DataType) == len(fp.Support)):
new_pts = self.get_points(fp, True)
if new_pts:
fp.Data = new_pts
if prop == "Parametrization":
self.execute(fp)
def onDocumentRestored(self, fp):
fp.setEditorMode("Data", 2)
fp.setEditorMode("DataType", 2)
class GordonProfileVP:
def __init__(self, vobj):
vobj.Proxy = self
self.select_state = True
self.active = False
def getIcon(self):
return TOOL_ICON
def attach(self, vobj):
self.Object = vobj.Object
self.active = False
self.select_state = vobj.Selectable
self.ip = None
def setEdit(self, vobj, mode=0):
if mode == 0 and check_pivy():
if vobj.Selectable:
self.select_state = True
vobj.Selectable = False
pts = list()
sl = list()
for ob, names in self.Object.Support:
for name in names:
sl.append((ob, (name,)))
shape_idx = 0
for i in range(len(self.Object.Data)):
p = self.Object.Data[i]
t = self.Object.DataType[i]
if t == 0:
pts.append(profile_editor.MarkerOnShape([p]))
elif t == 1:
pts.append(profile_editor.MarkerOnShape([p], sl[shape_idx]))
shape_idx += 1
for i in range(len(pts)): # p,t,f in zip(pts, self.Object.Tangents, self.Object.Flags):
if i < min(len(self.Object.Flags), len(self.Object.Tangents)):
if self.Object.Flags[i]:
pts[i].tangent = self.Object.Tangents[i]
self.ip = profile_editor.InterpoCurveEditor(pts, self.Object)
self.ip.periodic = self.Object.Periodic
self.ip.param_factor = self.Object.Parametrization
for i in range(min(len(self.Object.LinearSegments), len(self.ip.lines))):
self.ip.lines[i].tangent = self.Object.LinearSegments[i]
self.ip.lines[i].updateLine()
self.active = True
return True
return False
def unsetEdit(self, vobj, mode=0):
if isinstance(self.ip, profile_editor.InterpoCurveEditor) and check_pivy():
pts = list()
typ = list()
tans = list()
flags = list()
# original_links = self.Object.Support
new_links = list()
for p in self.ip.points:
if isinstance(p, profile_editor.MarkerOnShape):
pt = p.points[0]
pts.append(FreeCAD.Vector(pt[0], pt[1], pt[2]))
if p.sublink:
new_links.append(p.sublink)
typ.append(1)
else:
typ.append(0)
if p.tangent:
tans.append(p.tangent)
flags.append(True)
else:
tans.append(FreeCAD.Vector())
flags.append(False)
self.Object.Tangents = tans
self.Object.Flags = flags
self.Object.LinearSegments = [li.linear for li in self.ip.lines]
self.Object.DataType = typ
self.Object.Data = pts
self.Object.Support = new_links
vobj.Selectable = self.select_state
self.ip.quit()
self.ip = None
self.active = False
self.Object.Document.recompute()
return True
def doubleClicked(self, vobj):
if not hasattr(self, 'active'):
self.active = False
if not self.active:
self.active = True
# self.setEdit(vobj)
vobj.Document.setEdit(vobj)
else:
vobj.Document.resetEdit()
self.active = False
return True
def __getstate__(self):
return {"name": self.Object.Name}
def __setstate__(self, state):
self.Object = FreeCAD.ActiveDocument.getObject(state["name"])
return None
class GordonProfileCommand:
"""Creates a editable interpolation curve"""
def makeFeature(self, sub, pts, typ):
fp = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "Freehand BSpline")
GordonProfileFP(fp, sub, pts, typ)
GordonProfileVP(fp.ViewObject)
FreeCAD.Console.PrintMessage(__usage__)
FreeCAD.ActiveDocument.recompute()
FreeCADGui.SendMsgToActiveView("ViewFit")
fp.ViewObject.Document.setEdit(fp.ViewObject)
def Activated(self):
s = FreeCADGui.Selection.getSelectionEx()
try:
ordered = FreeCADGui.activeWorkbench().Selection
if ordered:
s = ordered
except AttributeError:
pass
sub = list()
pts = list()
for obj in s:
if obj.HasSubObjects:
# FreeCAD.Console.PrintMessage("object has subobjects %s\n"%str(obj.SubElementNames))
for n in obj.SubElementNames:
sub.append((obj.Object, [n]))
for p in obj.PickedPoints:
pts.append(p)
if len(pts) == 0:
pts = [FreeCAD.Vector(0, 0, 0), FreeCAD.Vector(5, 0, 0), FreeCAD.Vector(10, 0, 0)]
typ = [0, 0, 0]
elif len(pts) == 1:
pts.append(pts[0] + FreeCAD.Vector(5, 0, 0))
pts.append(pts[0] + FreeCAD.Vector(10, 0, 0))
typ = [1, 0, 0]
else:
typ = [1] * len(pts)
self.makeFeature(sub, pts, typ)
def IsActive(self):
if FreeCAD.ActiveDocument:
return True
else:
return False
def GetResources(self):
return {'Pixmap': TOOL_ICON,
'MenuText': __title__,
'ToolTip': "{}<br><br><b>Usage :</b><br>{}".format(__doc__, "<br>".join(__usage__.splitlines()))}
FreeCADGui.addCommand('gordon_profile', GordonProfileCommand())
-533
View File
@@ -1,533 +0,0 @@
from pivy import coin
#from pivy.utils import getPointOnScreen
def getPointOnScreen(render_manager, screen_pos, normal="camera", point=None):
"""get coordinates from pixel position"""
pCam = render_manager.getCamera()
vol = pCam.getViewVolume()
point = point or coin.SbVec3f(0, 0, 0)
if normal == "camera":
plane = vol.getPlane(10)
normal = plane.getNormal()
elif normal == "x":
normal = SbVec3f(1, 0, 0)
elif normal == "y":
normal = SbVec3f(0, 1, 0)
elif normal == "z":
normal = SbVec3f(0, 0, 1)
normal.normalize()
x, y = screen_pos
vp = render_manager.getViewportRegion()
size = vp.getViewportSize()
dX, dY = size
fRatio = vp.getViewportAspectRatio()
pX = float(x) / float(vp.getViewportSizePixels()[0])
pY = float(y) / float(vp.getViewportSizePixels()[1])
if (fRatio > 1.0):
pX = (pX - 0.5 * dX) * fRatio + 0.5 * dX
elif (fRatio < 1.0):
pY = (pY - 0.5 * dY) / fRatio + 0.5 * dY
plane = coin.SbPlane(normal, point)
line = coin.SbLine(*vol.projectPointToLine(coin.SbVec2f(pX,pY)))
pt = plane.intersect(line)
return(pt)
COLORS = {
"black": (0., 0., 0.),
"white": (1., 1., 1.),
"grey": (.5, .5, .5),
"red": (1., 0., 0.),
"green": (0., 1., 0.),
"blue": (0., 0., 1.),
"yellow": (1., 1., 0.),
"cyan": (0., 1., 1.),
"magenta":(1., 0., 1.)
}
class Object3D(coin.SoSeparator):
std_col = "black"
ovr_col = "red"
sel_col = "yellow"
non_col = "grey"
def __init__(self, points, dynamic=False):
super(Object3D, self).__init__()
self.data = coin.SoCoordinate3()
self.color = coin.SoMaterial()
self.set_color()
self += [self.color, self.data]
self.start_pos = None
self.dynamic = dynamic
# callback function lists
self.on_drag = []
self.on_drag_release = []
self.on_drag_start = []
self._delete = False
self._tmp_points = None
self.enabled = True
self.points = points
def set_disabled(self):
self.color.diffuseColor = COLORS[self.non_col]
self.enabled = False
def set_enabled(self):
self.color.diffuseColor = COLORS[self.std_col]
self.enabled = True
def set_color(self, col=None):
self.std_col = col or self.std_col
self.color.diffuseColor = COLORS[self.std_col]
@property
def points(self):
return self.data.point.getValues()
@points.setter
def points(self, points):
self.data.point.setValue(0, 0, 0)
self.data.point.setValues(0, len(points), points)
def set_mouse_over(self):
if self.enabled:
self.color.diffuseColor = COLORS[self.ovr_col]
def unset_mouse_over(self):
if self.enabled:
self.color.diffuseColor = COLORS[self.std_col]
def select(self):
if self.enabled:
self.color.diffuseColor = COLORS[self.sel_col]
def unselect(self):
if self.enabled:
self.color.diffuseColor = COLORS[self.std_col]
def drag(self, mouse_coords, fact=1.):
if self.enabled:
pts = self.points
for i, pt in enumerate(pts):
pt[0] = mouse_coords[0] * fact + self._tmp_points[i][0]
pt[1] = mouse_coords[1] * fact + self._tmp_points[i][1]
pt[2] = mouse_coords[2] * fact + self._tmp_points[i][2]
self.points = pts
for foo in self.on_drag:
foo()
def drag_release(self):
if self.enabled:
for foo in self.on_drag_release:
foo()
def drag_start(self):
self._tmp_points = self.points
if self.enabled:
for foo in self.on_drag_start:
foo()
@property
def drag_objects(self):
if self.enabled:
return [self]
def delete(self):
if self.enabled and not self._delete:
self._delete = True
def check_dependency(self):
pass
class Marker(Object3D):
def __init__(self, points, dynamic=False):
super(Marker, self).__init__(points, dynamic)
self.marker = coin.SoMarkerSet()
self.marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
self.addChild(self.marker)
class Line(Object3D):
def __init__(self, points, dynamic=False):
super(Line, self).__init__(points, dynamic)
self.drawstyle = coin.SoDrawStyle()
self.line = coin.SoLineSet()
self.addChild(self.drawstyle)
self.addChild(self.line)
class Point(Object3D):
def __init__(self, points, dynamic=False):
super(Point, self).__init__(points, dynamic)
self.drawstyle = coin.SoDrawStyle()
self.point = coin.SoPointSet()
self.addChild(self.drawstyle)
self.addChild(self.point)
class Polygon(Object3D):
def __init__(self, points, dynamic=False):
super(Polygon, self).__init__(points, dynamic)
self.polygon = coin.SoFaceSet()
self.addChild(self.polygon)
class Arrow(Line):
def __init__(self, points, dynamic=False, arrow_size=0.04, length=2):
super(Arrow, self).__init__(points, dynamic)
self.arrow_sep = coin.SoSeparator()
self.arrow_rot = coin.SoRotation()
self.arrow_scale = coin.SoScale()
self.arrow_translate = coin.SoTranslation()
self.arrow_scale.scaleFactor.setValue(arrow_size, arrow_size, arrow_size)
self.cone = coin.SoCone()
arrow_length = coin.SoScale()
arrow_length.scaleFactor = (1, length, 1)
arrow_origin = coin.SoTranslation()
arrow_origin.translation = (0, -1, 0)
self.arrow_sep += [self.arrow_translate, self.arrow_rot, self.arrow_scale]
self.arrow_sep += [arrow_length, arrow_origin, self.cone]
self += [self.arrow_sep]
self.set_arrow_direction()
def set_arrow_direction(self):
pts = np.array(self.points)
self.arrow_translate.translation = tuple(pts[-1])
direction = pts[-1] - pts[-2]
direction /= np.linalg.norm(direction)
_rot = coin.SbRotation()
_rot.setValue(coin.SbVec3f(0, 1, 0), coin.SbVec3f(*direction))
self.arrow_rot.rotation.setValue(_rot)
class InteractionSeparator(coin.SoSeparator):
pick_radius = 20
ctrl_keys = {"grab": "g",
"abort_grab": u"\uff1b",
"select_all": "a",
"delete": u"\uffff",
"axis_x": "x",
"axis_y": "y",
"axis_z": "z"}
def __init__(self, render_manager):
super(InteractionSeparator, self).__init__()
self.render_manager = render_manager
self.objects = coin.SoSeparator()
self.dynamic_objects = []
self.static_objects = []
self.over_object = None
self.selected_objects = []
self.drag_objects = []
self.on_drag = []
self.on_drag_release = []
self.on_drag_start = []
self._direction = None
self.events = coin.SoEventCallback()
self += self.events, self.objects
def register(self):
self._highlightCB = self.events.addEventCallback(
coin.SoLocation2Event.getClassTypeId(), self.highlightCB)
self._selectCB = self.events.addEventCallback(
coin.SoMouseButtonEvent.getClassTypeId(), self.selectCB)
self._grabCB = self.events.addEventCallback(
coin.SoMouseButtonEvent.getClassTypeId(), self.grabCB)
self._deleteCB = self.events.addEventCallback(
coin.SoKeyboardEvent.getClassTypeId(), self.deleteCB)
self._selectAllCB = self.events.addEventCallback(
coin.SoKeyboardEvent.getClassTypeId(), self.selectAllCB)
def unregister(self):
self.events.removeEventCallback(
coin.SoLocation2Event.getClassTypeId(), self._highlightCB)
self.events.removeEventCallback(
coin.SoMouseButtonEvent.getClassTypeId(), self._selectCB)
self.events.removeEventCallback(
coin.SoMouseButtonEvent.getClassTypeId(), self._grabCB)
self.events.removeEventCallback(
coin.SoKeyboardEvent.getClassTypeId(), self._deleteCB)
self.events.removeEventCallback(
coin.SoKeyboardEvent.getClassTypeId(), self._selectAllCB)
def addChild(self, child):
if hasattr(child, "dynamic"):
self.objects.addChild(child)
if child.dynamic:
self.dynamic_objects.append(child)
else:
self.static_objects.append(child)
else:
super(InteractionSeparator, self).addChild(child)
#-----------------------HIGHLIGHTING-----------------------#
# a SoLocation2Event calling a function which sends rays #
# int the scene. This will return the object the mouse is #
# currently hoovering. #
def highlightObject(self, obj):
if self.over_object:
self.over_object.unset_mouse_over()
self.over_object = obj
if self.over_object:
self.over_object.set_mouse_over()
self.colorSelected()
def highlightCB(self, attr, event_callback):
event = event_callback.getEvent()
pos = event.getPosition()
obj = self.sendRay(pos)
self.highlightObject(obj)
def sendRay(self, mouse_pos):
"""sends a ray through the scene and return the nearest entity"""
ray_pick = coin.SoRayPickAction(self.render_manager.getViewportRegion())
ray_pick.setPoint(coin.SbVec2s(*mouse_pos))
ray_pick.setRadius(InteractionSeparator.pick_radius)
ray_pick.setPickAll(True)
ray_pick.apply(self.render_manager.getSceneGraph())
picked_point = ray_pick.getPickedPointList()
return self.objByID(picked_point)
def objByID(self, picked_point):
for point in picked_point:
path = point.getPath()
length = path.getLength()
point = path.getNode(length - 2)
for o in self.dynamic_objects:
if point == o:
return(o)
# Code below was not working with python 2.7 (pb with getNodeId ?)
#point = list(filter(
#lambda ctrl: ctrl.getNodeId() == point.getNodeId(),
#self.dynamic_objects))
#if point != []:
#return point[0]
return None
#------------------------SELECTION------------------------#
def selectObject(self, obj, multi=False):
if not multi:
for o in self.selected_objects:
o.unselect()
self.selected_objects = []
if obj:
if obj in self.selected_objects:
self.selected_objects.remove(obj)
else:
self.selected_objects.append(obj)
self.colorSelected()
self.selectionChanged()
def selectCB(self, attr, event_callback):
event = event_callback.getEvent()
if (event.getState() == coin.SoMouseButtonEvent.DOWN and
event.getButton() == event.BUTTON1):
pos = event.getPosition()
obj = self.sendRay(pos)
self.selectObject(obj, event.wasCtrlDown())
def select_all_cb(self, event_callback):
event = event_callback.getEvent()
if (event.getKey() == ord(InteractionSeparator.ctrl_keys["select_all"])):
if event.getState() == event.DOWN:
if self.selected_objects:
for o in self.selected_objects:
o.unselect()
self.selected_objects = []
else:
for obj in self.objects:
if obj.dynamic:
self.selected_objects.append(obj)
self.ColorSelected()
self.selection_changed()
def deselect_all(self):
if self.selected_objects:
for o in self.selected_objects:
o.unselect()
self.selected_objects = []
def colorSelected(self):
for obj in self.selected_objects:
obj.select()
def selectionChanged(self):
pass
def selectAllCB(self, attr, event_callback):
event = event_callback.getEvent()
if (event.getKey() == ord(InteractionSeparator.ctrl_keys["select_all"])):
if event.getState() == event.DOWN:
if self.selected_objects:
for o in self.selected_objects:
o.unselect()
self.selected_objects = []
else:
for obj in self.dynamic_objects:
if obj.dynamic:
self.selected_objects.append(obj)
self.colorSelected()
self.selectionChanged()
#------------------------INTERACTION------------------------#
def cursor_pos(self, event):
pos = event.getPosition()
# print(list(getPointOnScreen1(self.render_manager, pos)))
return getPointOnScreen(self.render_manager, pos)
def constrained_vector(self, vector):
if self._direction is None:
return vector
if self._direction == InteractionSeparator.ctrl_keys["axis_x"]:
return [vector[0], 0, 0]
elif self._direction == InteractionSeparator.ctrl_keys["axis_y"]:
return [0, vector[1], 0]
elif self._direction == InteractionSeparator.ctrl_keys["axis_z"]:
return [0, 0, vector[2]]
def grabCB(self, attr, event_callback):
# press grab key to move an entity
event = event_callback.getEvent()
# get all drag objects, every selected object can add some drag objects
# but the eventhandler is not allowed to call the drag twice on an object
#if event.getKey() == ord(InteractionSeparator.ctrl_keys["grab"]):
if (event.getState() == coin.SoMouseButtonEvent.DOWN and
event.getButton() == event.BUTTON1):
pos = event.getPosition()
obj = self.sendRay(pos)
if obj:
#if not obj in self.selected_objects:
#self.selectObject(obj, event.wasCtrlDown())
self.drag_objects = set()
for i in self.selected_objects:
for j in i.drag_objects:
self.drag_objects.add(j)
# check if something is selected
if self.drag_objects:
# first delete the selection_cb, and higlight_cb
self.unregister()
# now add a callback that calls the dragfunction of the selected entities
self.start_pos = self.cursor_pos(event)
self._dragCB = self.events.addEventCallback(
coin.SoEvent.getClassTypeId(), self.dragCB)
for obj in self.drag_objects:
obj.drag_start()
for foo in self.on_drag_start:
foo()
def dragCB(self, attr, event_callback, force=False):
event = event_callback.getEvent()
b = ""
s = ""
if type(event) == coin.SoMouseButtonEvent:
if event.getButton() == coin.SoMouseButtonEvent.BUTTON1:
b = "mb1"
elif event.getButton() == coin.SoMouseButtonEvent.BUTTON2:
b = "mb2"
if event.getState() == coin.SoMouseButtonEvent.UP:
s = "up"
elif event.getState() == coin.SoMouseButtonEvent.DOWN:
s = "down"
import FreeCAD
FreeCAD.Console.PrintMessage("{} {}\n".format(b,s))
if ((type(event) == coin.SoMouseButtonEvent and
event.getState() == coin.SoMouseButtonEvent.UP
and event.getButton() == coin.SoMouseButtonEvent.BUTTON1) or
force):
self.register()
if self._dragCB:
self.events.removeEventCallback(
coin.SoEvent.getClassTypeId(), self._dragCB)
self._direction = None
self._dragCB = None
self.start_pos = None
for obj in self.drag_objects:
obj.drag_release()
for foo in self.on_drag_release:
foo()
self.drag_objects = []
elif (type(event) == coin.SoKeyboardEvent and
event.getState() == coin.SoMouseButtonEvent.DOWN):
if event.getKey() == InteractionSeparator.ctrl_keys["abort_grab"]: # esc
for obj in self.drag_objects:
obj.drag([0, 0, 0], 1) # set back to zero
self.dragCB(attr, event_callback, force=True)
return
try:
key = chr(event.getKey())
except ValueError:
# there is no character for this value
key = "_"
if key in [InteractionSeparator.ctrl_keys["axis_x"],
InteractionSeparator.ctrl_keys["axis_y"],
InteractionSeparator.ctrl_keys["axis_z"]] and key != self._direction:
self._direction = key
else:
self._direction = None
diff = self.cursor_pos(event) - self.start_pos
diff = self.constrained_vector(diff)
for obj in self.drag_objects:
obj.drag(diff, 1)
for foo in self.on_drag:
foo()
elif type(event) == coin.SoLocation2Event:
fact = 0.1 if event.wasShiftDown() else 1.
diff = self.cursor_pos(event) - self.start_pos
diff = self.constrained_vector(diff)
for obj in self.drag_objects:
obj.drag(diff, fact)
for foo in self.on_drag:
foo()
def deleteCB(self, attr, event_callback):
event = event_callback.getEvent()
# get all drag objects, every selected object can add some drag objects
# but the eventhandler is not allowed to call the drag twice on an object
if event.getKey() == ord(InteractionSeparator.ctrl_keys["delete"]) and (event.getState() == 1):
self.removeSelected()
def removeSelected(self):
temp = []
for i in self.selected_objects:
i.delete()
for i in self.dynamic_objects + self.static_objects:
i.check_dependency() #dependency length max = 1
for i in self.dynamic_objects + self.static_objects:
if i._delete:
temp.append(i)
self.selected_objects = []
self.over_object = None
self.selectionChanged()
for i in temp:
if i in self.dynamic_objects:
self.dynamic_objects.remove(i)
else:
self.static_objects.remove(i)
self.objects.removeChild(i)
del(i)
self.selectionChanged()
def removeAllChildren(self):
for i in self.dynamic_objects:
i.delete()
self.dynamic_objects = []
self.static_objects = []
self.selected_objects = []
self.over_object = None
super(InteractionSeparator, self).removeAllChildren()
-501
View File
@@ -1,501 +0,0 @@
# from curve workbench
import FreeCAD
import FreeCADGui
import Part
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
from pivy import coin
from Utils import graphics
def parameterization(points, a, closed):
"""Computes a knot Sequence for a set of points
fac (0-1) : parameterization factor
fac=0 -> Uniform / fac=0.5 -> Centripetal / fac=1.0 -> Chord-Length"""
pts = points.copy()
if closed and pts[0].distanceToPoint(pts[-1]) > 1e-7: # we need to add the first point as the end point
pts.append(pts[0])
params = [0]
for i in range(1, len(pts)):
p = pts[i] - pts[i - 1]
if isinstance(p, FreeCAD.Vector):
le = p.Length
else:
le = p.length()
pl = pow(le, a)
params.append(params[-1] + pl)
return params
class ConnectionMarker(graphics.Marker):
def __init__(self, points):
super(ConnectionMarker, self).__init__(points, True)
class MarkerOnShape(graphics.Marker):
def __init__(self, points, sh=None):
super(MarkerOnShape, self).__init__(points, True)
self._shape = None
self._sublink = None
self._tangent = None
self._translate = coin.SoTranslation()
self._text_font = coin.SoFont()
self._text_font.name = "Arial:Bold"
self._text_font.size = 13.0
self._text = coin.SoText2()
self._text_switch = coin.SoSwitch()
self._text_switch.addChild(self._translate)
self._text_switch.addChild(self._text_font)
self._text_switch.addChild(self._text)
self.on_drag_start.append(self.add_text)
self.on_drag_release.append(self.remove_text)
self.addChild(self._text_switch)
if isinstance(sh, Part.Shape):
self.snap_shape = sh
elif isinstance(sh, (tuple, list)):
self.sublink = sh
def subshape_from_sublink(self, o):
name = o[1][0]
print(name, " selected")
if 'Vertex' in name:
n = eval(name.lstrip('Vertex'))
return o[0].Shape.Vertexes[n - 1]
elif 'Edge' in name:
n = eval(name.lstrip('Edge'))
return o[0].Shape.Edges[n - 1]
elif 'Face' in name:
n = eval(name.lstrip('Face'))
return o[0].Shape.Faces[n - 1]
def add_text(self):
self._text_switch.whichChild = coin.SO_SWITCH_ALL
self.on_drag.append(self.update_text)
def remove_text(self):
self._text_switch.whichChild = coin.SO_SWITCH_NONE
self.on_drag.remove(self.update_text)
def update_text(self):
p = self.points[0]
coords = ['{: 9.3f}'.format(p[0]), '{: 9.3f}'.format(p[1]), '{: 9.3f}'.format(p[2])]
self._translate.translation = p
self._text.string.setValues(0, 3, coords)
@property
def tangent(self):
return self._tangent
@tangent.setter
def tangent(self, t):
if isinstance(t, FreeCAD.Vector):
if t.Length > 1e-7:
self._tangent = t
self._tangent.normalize()
self.marker.markerIndex = coin.SoMarkerSet.DIAMOND_FILLED_9_9
else:
self._tangent = None
self.marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
else:
self._tangent = None
self.marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
@property
def snap_shape(self):
return self._shape
@snap_shape.setter
def snap_shape(self, sh):
if isinstance(sh, Part.Shape):
self._shape = sh
else:
self._shape = None
self.alter_color()
@property
def sublink(self):
return self._sublink
@sublink.setter
def sublink(self, sl):
if isinstance(sl, (tuple, list)) and not (sl == self._sublink):
self._shape = self.subshape_from_sublink(sl)
self._sublink = sl
else:
self._shape = None
self._sublink = None
self.alter_color()
def alter_color(self):
if isinstance(self._shape, Part.Vertex):
self.set_color("white")
elif isinstance(self._shape, Part.Edge):
self.set_color("cyan")
elif isinstance(self._shape, Part.Face):
self.set_color("magenta")
else:
self.set_color("black")
def __repr__(self):
return "MarkerOnShape({})".format(self._shape)
def drag(self, mouse_coords, fact=1.):
if self.enabled:
pts = self.points
for i, p in enumerate(pts):
p[0] = mouse_coords[0] * fact + self._tmp_points[i][0]
p[1] = mouse_coords[1] * fact + self._tmp_points[i][1]
p[2] = mouse_coords[2] * fact + self._tmp_points[i][2]
if self._shape:
v = Part.Vertex(p[0], p[1], p[2])
proj = v.distToShape(self._shape)[1][0][1]
# FreeCAD.Console.PrintMessage("%s -> %s\n"%(p.getValue(), proj))
p[0] = proj.x
p[1] = proj.y
p[2] = proj.z
self.points = pts
for foo in self.on_drag:
foo()
class ConnectionPolygon(graphics.Polygon):
std_col = "green"
def __init__(self, markers):
super(ConnectionPolygon, self).__init__(
sum([m.points for m in markers], []), True)
self.markers = markers
for m in self.markers:
m.on_drag.append(self.updatePolygon)
def updatePolygon(self):
self.points = sum([m.points for m in self.markers], [])
@property
def drag_objects(self):
return self.markers
def check_dependency(self):
if any([m._delete for m in self.markers]):
self.delete()
class ConnectionLine(graphics.Line):
def __init__(self, markers):
super(ConnectionLine, self).__init__(
sum([m.points for m in markers], []), True)
self.markers = markers
self._linear = False
for m in self.markers:
m.on_drag.append(self.updateLine)
def updateLine(self):
self.points = sum([m.points for m in self.markers], [])
if self._linear:
p1 = self.markers[0].points[0]
p2 = self.markers[-1].points[0]
t = p2 - p1
tan = FreeCAD.Vector(t[0], t[1], t[2])
for m in self.markers:
m.tangent = tan
@property
def linear(self):
return self._linear
@linear.setter
def linear(self, b):
self._linear = bool(b)
@property
def drag_objects(self):
return self.markers
def check_dependency(self):
if any([m._delete for m in self.markers]):
self.delete()
class Edit(object):
def __init__(self, points=[], obj=None):
self.points = list()
self.lines = list()
self.obj = obj
self.root_inserted = False
self.root = None
self.editing = None
# event callbacks
self.selection_callback = None
self._keyPressedCB = None
self._mouseMovedCB = None
self._mousePressedCB = None
for p in points:
if isinstance(p, FreeCAD.Vector):
self.points.append(MarkerOnShape([p]))
elif isinstance(p, (tuple, list)):
self.points.append(MarkerOnShape([p[0]], p[1]))
elif isinstance(p, (MarkerOnShape, ConnectionMarker)):
self.points.append(p)
else:
FreeCAD.Console.PrintError("InterpoCurveEditor : bad input")
# Setup coin objects
if self.obj:
self.guidoc = self.obj.ViewObject.Document
else:
if not FreeCADGui.ActiveDocument:
FreeCAD.newDocument("New")
self.guidoc = FreeCADGui.ActiveDocument
self.view = self.guidoc.ActiveView
self.rm = self.view.getViewer().getSoRenderManager()
self.sg = self.view.getSceneGraph()
self.setupInteractionSeparator()
# Callbacks
#self.unregister_editing_callbacks()
#self.register_editing_callbacks()
def setupInteractionSeparator(self):
if self.root_inserted:
self.sg.removeChild(self.root)
self.root = graphics.InteractionSeparator(self.rm)
self.root.setName("InteractionSeparator")
self.root.pick_radius = 40
# Populate root node
self.root += self.points
self.build_lines()
self.root += self.lines
# set FreeCAD color scheme
for o in self.points + self.lines:
o.ovr_col = "yellow"
o.sel_col = "green"
self.root.register()
self.sg.addChild(self.root)
self.root_inserted = True
self.root.selected_objects = list()
def build_lines(self):
for i in range(len(self.points) - 1):
line = ConnectionLine([self.points[i], self.points[i + 1]])
line.set_color("blue")
self.lines.append(line)
# -------------------------------------------------------------------------
# SCENE EVENTS CALLBACKS
# -------------------------------------------------------------------------
def register_editing_callbacks(self):
""" Register editing callbacks (former action function) """
if self._keyPressedCB is None:
self._keyPressedCB = self.root.events.addEventCallback(coin.SoKeyboardEvent.getClassTypeId(), self.keyPressed)
if self._mousePressedCB is None:
self._mousePressedCB = self.root.events.addEventCallback(coin.SoMouseButtonEvent.getClassTypeId(), self.mousePressed)
if self._mouseMovedCB is None:
self._mouseMovedCB = self.root.events.addEventCallback(coin.SoLocation2Event.getClassTypeId(), self.mouseMoved)
def unregister_editing_callbacks(self):
""" Remove callbacks used during editing if they exist """
if self._keyPressedCB:
self.root.events.removeEventCallback(coin.SoKeyboardEvent.getClassTypeId(), self._keyPressedCB)
self._keyPressedCB = None
if self._mousePressedCB:
self.root.events.removeEventCallback(coin.SoMouseButtonEvent.getClassTypeId(), self._mousePressedCB)
self._mousePressedCB = None
if self._mouseMovedCB:
self.root.events.removeEventCallback(coin.SoLocation2Event.getClassTypeId(), self._mouseMovedCB)
self._mouseMovedCB = None
# -------------------------------------------------------------------------
# SCENE EVENT HANDLERS
# -------------------------------------------------------------------------
def keyPressed(self, attr, event_callback):
event = event_callback.getEvent()
if event.getState() == event.UP:
#FreeCAD.Console.PrintMessage("Key pressed : %s\n"%event.getKey())
if event.getKey() == ord("i"):
self.subdivide()
elif event.getKey() == ord("q"):# or event.getKey() == ord(65307):
if self.obj:
self.obj.ViewObject.Proxy.doubleClicked(self.obj.ViewObject)
else:
self.quit()
elif event.getKey() == ord("s"):
sel = FreeCADGui.Selection.getSelectionEx()
tup = None
if len(sel) == 1:
tup = (sel[0].Object, sel[0].SubElementNames)
for i in range(len(self.root.selected_objects)):
if isinstance(self.root.selected_objects[i], MarkerOnShape):
self.root.selected_objects[i].sublink = tup
#FreeCAD.Console.PrintMessage("Snapped to {}\n".format(str(self.root.selected_objects[i].sublink)))
self.root.selected_objects[i].drag_start()
self.root.selected_objects[i].drag((0, 0, 0.))
self.root.selected_objects[i].drag_release()
elif (event.getKey() == 65535) or (event.getKey() == 65288): # Suppr or Backspace
# FreeCAD.Console.PrintMessage("Some objects have been deleted\n")
pts = list()
for o in self.root.dynamic_objects:
if isinstance(o, MarkerOnShape):
pts.append(o)
self.points = pts
self.setupInteractionSeparator()
def mousePressed(self, attr, event_callback):
""" Mouse button event handler, calls: startEditing, endEditing, addPoint, delPoint """
event = event_callback.getEvent()
if (event.getState() == coin.SoMouseButtonEvent.DOWN) and (event.getButton() == event.BUTTON1): # left click
if not event.wasAltDown():
''' do something '''
if self.editing is None:
''' do something'''
else:
self.endEditing(self.obj, self.editing)
elif event.wasAltDown(): # left click with ctrl down
self.display_tracker_menu(event)
elif (event.getState() == coin.SoMouseButtonEvent.DOWN) and (event.getButton() == event.BUTTON2): # right click
self.display_tracker_menu(event)
def mouseMoved(self, attr, event_callback):
""" Execute as callback for mouse movement. Update tracker position and update preview ghost. """
event = event_callback.getEvent()
pos = event.getPosition()
'''
if self.editing is not None:
self.updateTrackerAndGhost(event)
else:
# look for a node in mouse position and highlight it
pos = event.getPosition()
node = self.getEditNode(pos)
ep = self.getEditNodeIndex(node)
if ep is not None:
if self.overNode is not None:
self.overNode.setColor(COLORS["default"])
self.trackers[str(node.objectName.getValue())][ep].setColor(COLORS["red"])
self.overNode = self.trackers[str(node.objectName.getValue())][ep]
print("show menu")
# self.display_tracker_menu(event)
else:
if self.overNode is not None:
self.overNode.setColor(COLORS["default"])
self.overNode = None
'''
def endEditing(self, obj, nodeIndex=None, v=None):
self.editing = None
# ------------------------------------------------------------------------
# DRAFT EDIT Context menu
# ------------------------------------------------------------------------
def display_tracker_menu(self, event):
self.tracker_menu = QtGui.QMenu()
self.event = event
actions = None
actions = ["add point"]
'''
if self.overNode:
# if user is over a node
doc = self.overNode.get_doc_name()
obj = App.getDocument(doc).getObject(self.overNode.get_obj_name())
ep = self.overNode.get_subelement_index()
obj_gui_tools = self.get_obj_gui_tools(obj)
if obj_gui_tools:
actions = obj_gui_tools.get_edit_point_context_menu(obj, ep)
else:
# try if user is over an edited object
pos = self.event.getPosition()
obj = self.get_selected_obj_at_position(pos)
if utils.get_type(obj) in ["Line", "Wire", "BSpline", "BezCurve"]:
actions = ["add point"]
elif utils.get_type(obj) in ["Circle"] and obj.FirstAngle != obj.LastAngle:
actions = ["invert arc"]
if actions is None:
return
'''
for a in actions:
self.tracker_menu.addAction(a)
self.tracker_menu.popup(FreeCADGui.getMainWindow().cursor().pos())
QtCore.QObject.connect(self.tracker_menu,
QtCore.SIGNAL("triggered(QAction *)"),
self.evaluate_menu_action)
def evaluate_menu_action(self, labelname):
action_label = str(labelname.text())
doc = None
obj = None
idx = None
if action_label == "add point":
self.addPoint(self.event)
del self.event
# -------------------------------------------------------------------------
# EDIT functions
# -------------------------------------------------------------------------
def addPoint(self, event):
''' add point to the end '''
pos = event.getPosition()
pts = self.points.copy()
new_select = list()
point = FreeCAD.Vector(pos)
mark = MarkerOnShape([point])
pts.append(mark)
new_select.append(mark)
self.points = pts
self.setupInteractionSeparator()
self.root.selected_objects = new_select
def subdivide(self):
# get selected lines and subdivide them
pts = list()
new_select = list()
for o in self.lines:
#FreeCAD.Console.PrintMessage("object %s\n"%str(o))
if isinstance(o, ConnectionLine):
pts.append(o.markers[0])
if o in self.root.selected_objects:
#idx = self.lines.index(o)
#FreeCAD.Console.PrintMessage("Subdividing line #{}\n".format(idx))
p1 = o.markers[0].points[0]
p2 = o.markers[1].points[0]
midpar = (FreeCAD.Vector(p1) + FreeCAD.Vector(p2)) / 2.0
mark = MarkerOnShape([midpar])
pts.append(mark)
new_select.append(mark)
pts.append(self.points[-1])
self.points = pts
self.setupInteractionSeparator()
self.root.selected_objects = new_select
return True
def quit(self):
self.unregister_editing_callbacks()
self.root.unregister()
self.sg.removeChild(self.root)
self.root_inserted = False
-533
View File
@@ -1,533 +0,0 @@
import FreeCAD
import FreeCADGui
import Part
from freecad.Curves import graphics
from pivy import coin
# from graphics import COLORS
# FreeCAD.Console.PrintMessage("Using local Pivy.graphics library\n")
def parameterization(points, a, closed):
"""Computes a knot Sequence for a set of points
fac (0-1) : parameterization factor
fac=0 -> Uniform / fac=0.5 -> Centripetal / fac=1.0 -> Chord-Length"""
pts = points.copy()
if closed and pts[0].distanceToPoint(pts[-1]) > 1e-7: # we need to add the first point as the end point
pts.append(pts[0])
params = [0]
for i in range(1, len(pts)):
p = pts[i] - pts[i - 1]
if isinstance(p, FreeCAD.Vector):
le = p.Length
else:
le = p.length()
pl = pow(le, a)
params.append(params[-1] + pl)
return params
class ConnectionMarker(graphics.Marker):
def __init__(self, points):
super(ConnectionMarker, self).__init__(points, True)
class MarkerOnShape(graphics.Marker):
def __init__(self, points, sh=None):
super(MarkerOnShape, self).__init__(points, True)
self._shape = None
self._sublink = None
self._tangent = None
self._translate = coin.SoTranslation()
self._text_font = coin.SoFont()
self._text_font.name = "Arial:Bold"
self._text_font.size = 13.0
self._text = coin.SoText2()
self._text_switch = coin.SoSwitch()
self._text_switch.addChild(self._translate)
self._text_switch.addChild(self._text_font)
self._text_switch.addChild(self._text)
self.on_drag_start.append(self.add_text)
self.on_drag_release.append(self.remove_text)
self.addChild(self._text_switch)
if isinstance(sh, Part.Shape):
self.snap_shape = sh
elif isinstance(sh, (tuple, list)):
self.sublink = sh
def subshape_from_sublink(self, o):
name = o[1][0]
if 'Vertex' in name:
n = eval(name.lstrip('Vertex'))
return(o[0].Shape.Vertexes[n - 1])
elif 'Edge' in name:
n = eval(name.lstrip('Edge'))
return(o[0].Shape.Edges[n - 1])
elif 'Face' in name:
n = eval(name.lstrip('Face'))
return(o[0].Shape.Faces[n - 1])
def add_text(self):
self._text_switch.whichChild = coin.SO_SWITCH_ALL
self.on_drag.append(self.update_text)
def remove_text(self):
self._text_switch.whichChild = coin.SO_SWITCH_NONE
self.on_drag.remove(self.update_text)
def update_text(self):
p = self.points[0]
coords = ['{: 9.3f}'.format(p[0]), '{: 9.3f}'.format(p[1]), '{: 9.3f}'.format(p[2])]
self._translate.translation = p
self._text.string.setValues(0, 3, coords)
@property
def tangent(self):
return self._tangent
@tangent.setter
def tangent(self, t):
if isinstance(t, FreeCAD.Vector):
if t.Length > 1e-7:
self._tangent = t
self._tangent.normalize()
self.marker.markerIndex = coin.SoMarkerSet.DIAMOND_FILLED_9_9
else:
self._tangent = None
self.marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
else:
self._tangent = None
self.marker.markerIndex = coin.SoMarkerSet.CIRCLE_FILLED_9_9
@property
def snap_shape(self):
return self._shape
@snap_shape.setter
def snap_shape(self, sh):
if isinstance(sh, Part.Shape):
self._shape = sh
else:
self._shape = None
self.alter_color()
@property
def sublink(self):
return self._sublink
@sublink.setter
def sublink(self, sl):
if isinstance(sl, (tuple, list)) and not (sl == self._sublink):
self._shape = self.subshape_from_sublink(sl)
self._sublink = sl
else:
self._shape = None
self._sublink = None
self.alter_color()
def alter_color(self):
if isinstance(self._shape, Part.Vertex):
self.set_color("white")
elif isinstance(self._shape, Part.Edge):
self.set_color("cyan")
elif isinstance(self._shape, Part.Face):
self.set_color("magenta")
else:
self.set_color("black")
def __repr__(self):
return("MarkerOnShape({})".format(self._shape))
def drag(self, mouse_coords, fact=1.):
if self.enabled:
pts = self.points
for i, p in enumerate(pts):
p[0] = mouse_coords[0] * fact + self._tmp_points[i][0]
p[1] = mouse_coords[1] * fact + self._tmp_points[i][1]
p[2] = mouse_coords[2] * fact + self._tmp_points[i][2]
if self._shape:
v = Part.Vertex(p[0], p[1], p[2])
proj = v.distToShape(self._shape)[1][0][1]
# FreeCAD.Console.PrintMessage("%s -> %s\n"%(p.getValue(), proj))
p[0] = proj.x
p[1] = proj.y
p[2] = proj.z
self.points = pts
for foo in self.on_drag:
foo()
class ConnectionPolygon(graphics.Polygon):
std_col = "green"
def __init__(self, markers):
super(ConnectionPolygon, self).__init__(
sum([m.points for m in markers], []), True)
self.markers = markers
for m in self.markers:
m.on_drag.append(self.updatePolygon)
def updatePolygon(self):
self.points = sum([m.points for m in self.markers], [])
@property
def drag_objects(self):
return self.markers
def check_dependency(self):
if any([m._delete for m in self.markers]):
self.delete()
class ConnectionLine(graphics.Line):
def __init__(self, markers):
super(ConnectionLine, self).__init__(
sum([m.points for m in markers], []), True)
self.markers = markers
self._linear = False
for m in self.markers:
m.on_drag.append(self.updateLine)
def updateLine(self):
self.points = sum([m.points for m in self.markers], [])
if self._linear:
p1 = self.markers[0].points[0]
p2 = self.markers[-1].points[0]
t = p2 - p1
tan = FreeCAD.Vector(t[0], t[1], t[2])
for m in self.markers:
m.tangent = tan
@property
def linear(self):
return self._linear
@linear.setter
def linear(self, b):
self._linear = bool(b)
@property
def drag_objects(self):
return self.markers
def check_dependency(self):
if any([m._delete for m in self.markers]):
self.delete()
class InterpoCurveEditor(object):
"""Interpolation curve free-hand editor
my_editor = InterpoCurveEditor([points], obj)
obj is the FreeCAD object that will receive
the curve shape at the end of editing.
points can be :
- Vector (free point)
- (Vector, shape) (point on shape)"""
def __init__(self, points=[], fp=None):
self.points = list()
self.curve = Part.BSplineCurve()
self.fp = fp
self.root_inserted = False
self.periodic = False
self.param_factor = 1.0
# self.support = None # Not yet implemented
for p in points:
if isinstance(p, FreeCAD.Vector):
self.points.append(MarkerOnShape([p]))
elif isinstance(p, (tuple, list)):
self.points.append(MarkerOnShape([p[0]], p[1]))
elif isinstance(p, (MarkerOnShape, ConnectionMarker)):
self.points.append(p)
else:
FreeCAD.Console.PrintError("InterpoCurveEditor : bad input")
# Setup coin objects
if self.fp:
self.guidoc = self.fp.ViewObject.Document
else:
if not FreeCADGui.ActiveDocument:
FreeCAD.newDocument("New")
self.guidoc = FreeCADGui.ActiveDocument
self.view = self.guidoc.ActiveView
self.rm = self.view.getViewer().getSoRenderManager()
self.sg = self.view.getSceneGraph()
self.setup_InteractionSeparator()
self.update_curve()
def setup_InteractionSeparator(self):
if self.root_inserted:
self.sg.removeChild(self.root)
self.root = graphics.InteractionSeparator(self.rm)
self.root.setName("InteractionSeparator")
# self.root.ovr_col = "yellow"
# self.root.sel_col = "green"
self.root.pick_radius = 40
self.root.on_drag.append(self.update_curve)
# Keyboard callback
# self.events = coin.SoEventCallback()
self._controlCB = self.root.events.addEventCallback(coin.SoKeyboardEvent.getClassTypeId(), self.controlCB)
# populate root node
# self.root.addChild(self.events)
self.root += self.points
self.build_lines()
self.root += self.lines
# set FreeCAD color scheme
for o in self.points + self.lines:
o.ovr_col = "yellow"
o.sel_col = "green"
self.root.register()
self.sg.addChild(self.root)
self.root_inserted = True
self.root.selected_objects = list()
def compute_tangents(self):
tans = list()
flags = list()
for i in range(len(self.points)):
if isinstance(self.points[i].snap_shape, Part.Face):
for vec in self.points[i].points:
u, v = self.points[i].snap_shape.Surface.parameter(FreeCAD.Vector(vec))
norm = self.points[i].snap_shape.normalAt(u, v)
cp = self.curve.parameter(FreeCAD.Vector(vec))
t = self.curve.tangent(cp)[0]
pl = Part.Plane(FreeCAD.Vector(), norm)
ci = Part.Geom2d.Circle2d()
ci.Radius = t.Length * 2
w = Part.Wire([ci.toShape(pl)])
f = Part.Face(w)
# proj = f.project([Part.Vertex(t)])
proj = Part.Vertex(t).distToShape(f)[1][0][1]
# pt = proj.Vertexes[0].Point
# FreeCAD.Console.PrintMessage("Projection %s -> %s\n"%(t, proj))
if proj.Length > 1e-7:
tans.append(proj)
flags.append(True)
else:
tans.append(FreeCAD.Vector(1, 0, 0))
flags.append(False)
elif self.points[i].tangent:
for j in range(len(self.points[i].points)):
tans.append(self.points[i].tangent)
flags.append(True)
else:
for j in range(len(self.points[i].points)):
tans.append(FreeCAD.Vector(0, 0, 0))
flags.append(False)
return(tans, flags)
def update_curve(self):
pts = list()
for p in self.points:
pts += p.points
# FreeCAD.Console.PrintMessage("pts :\n%s\n"%str(pts))
if len(pts) > 1:
fac = self.param_factor
if self.fp:
fac = self.fp.Parametrization
params = parameterization(pts, fac, self.periodic)
self.curve.interpolate(Points=pts, Parameters=params, PeriodicFlag=self.periodic)
tans, flags = self.compute_tangents()
if any(flags):
if (len(tans) == len(pts)) and (len(flags) == len(pts)):
self.curve.interpolate(Points=pts, Parameters=params, PeriodicFlag=self.periodic, Tangents=tans, TangentFlags=flags)
if self.fp:
self.fp.Shape = self.curve.toShape()
def build_lines(self):
self.lines = list()
for i in range(len(self.points) - 1):
line = ConnectionLine([self.points[i], self.points[i + 1]])
line.set_color("blue")
self.lines.append(line)
def controlCB(self, attr, event_callback):
event = event_callback.getEvent()
if event.getState() == event.UP:
# FreeCAD.Console.PrintMessage("Key pressed : %s\n"%event.getKey())
if event.getKey() == ord("i"):
self.subdivide()
elif event.getKey() == ord("p"):
self.set_planar()
elif event.getKey() == ord("t"):
self.set_tangents()
elif event.getKey() == ord("q"):
if self.fp:
self.fp.ViewObject.Proxy.doubleClicked(self.fp.ViewObject)
else:
self.quit()
elif event.getKey() == ord("s"):
sel = FreeCADGui.Selection.getSelectionEx()
tup = None
if len(sel) == 1:
tup = (sel[0].Object, sel[0].SubElementNames)
for i in range(len(self.root.selected_objects)):
if isinstance(self.root.selected_objects[i], MarkerOnShape):
self.root.selected_objects[i].sublink = tup
FreeCAD.Console.PrintMessage("Snapped to {}\n".format(str(self.root.selected_objects[i].sublink)))
self.root.selected_objects[i].drag_start()
self.root.selected_objects[i].drag((0, 0, 0.))
self.root.selected_objects[i].drag_release()
self.update_curve()
elif event.getKey() == ord("l"):
self.toggle_linear()
elif (event.getKey() == 65535) or (event.getKey() == 65288): # Suppr or Backspace
# FreeCAD.Console.PrintMessage("Some objects have been deleted\n")
pts = list()
for o in self.root.dynamic_objects:
if isinstance(o, MarkerOnShape):
pts.append(o)
self.points = pts
self.setup_InteractionSeparator()
self.update_curve()
def toggle_linear(self):
for o in self.root.selected_objects:
if isinstance(o, ConnectionLine):
o.linear = not o.linear
i = self.lines.index(o)
if i > 0:
self.lines[i - 1].linear = False
if i < len(self.lines) - 1:
self.lines[i + 1].linear = False
o.updateLine()
o.drag_start()
o.drag((0, 0, 0.00001))
o.drag_release()
self.update_curve()
def set_tangents(self):
# view_dir = FreeCAD.Vector(0, 0, 1)
view_dir = FreeCADGui.ActiveDocument.ActiveView.getViewDirection()
markers = list()
for o in self.root.selected_objects:
if isinstance(o, MarkerOnShape):
markers.append(o)
elif isinstance(o, ConnectionLine):
markers.extend(o.markers)
if len(markers) > 0:
for m in markers:
if m.tangent:
m.tangent = None
else:
i = self.points.index(m)
if i == 0:
m.tangent = -view_dir
else:
m.tangent = view_dir
self.update_curve()
def set_planar(self):
# view_dir = FreeCAD.Vector(0, 0, 1)
view_dir = FreeCADGui.ActiveDocument.ActiveView.getViewDirection()
markers = list()
for o in self.root.selected_objects:
if isinstance(o, MarkerOnShape):
markers.append(o)
elif isinstance(o, ConnectionLine):
markers.extend(o.markers)
if len(markers) > 2:
vec0 = markers[0].points[0]
vec1 = markers[-1].points[0]
p0 = FreeCAD.Vector(vec0[0], vec0[1], vec0[2])
p1 = FreeCAD.Vector(vec1[0], vec1[1], vec1[2])
pl = Part.Plane(p0, p1, p1 + view_dir)
for o in markers:
if isinstance(o.snap_shape, Part.Vertex):
FreeCAD.Console.PrintMessage("Snapped to Vertex\n")
elif isinstance(o.snap_shape, Part.Edge):
FreeCAD.Console.PrintMessage("Snapped to Edge\n")
c = o.snap_shape.Curve
pts = pl.intersect(c)[0]
new_pts = list()
for ip in o.points:
iv = FreeCAD.Vector(ip[0], ip[1], ip[2])
dmin = 1e50
new = None
for op in pts:
ov = FreeCAD.Vector(op.X, op.Y, op.Z)
if iv.distanceToPoint(ov) < dmin:
dmin = iv.distanceToPoint(ov)
new = ov
new_pts.append(new)
o.points = new_pts
elif isinstance(o.snap_shape, Part.Face):
FreeCAD.Console.PrintMessage("Snapped to Face\n")
s = o.snap_shape.Surface
cvs = pl.intersect(s)
new_pts = list()
for ip in o.points:
iv = Part.Vertex(FreeCAD.Vector(ip[0], ip[1], ip[2]))
dmin = 1e50
new = None
for c in cvs:
e = c.toShape()
d, pts, info = iv.distToShape(e)
if d < dmin:
dmin = d
new = pts[0][1]
new_pts.append(new)
o.points = new_pts
else:
FreeCAD.Console.PrintMessage("Not snapped\n")
new_pts = list()
for ip in o.points:
iv = FreeCAD.Vector(ip[0], ip[1], ip[2])
u, v = pl.parameter(iv)
new_pts.append(pl.value(u, v))
o.points = new_pts
for li in self.lines:
li.updateLine()
self.update_curve()
def subdivide(self):
# get selected lines and subdivide them
pts = list()
new_select = list()
for o in self.lines:
# FreeCAD.Console.PrintMessage("object %s\n"%str(o))
if isinstance(o, ConnectionLine):
pts.append(o.markers[0])
if o in self.root.selected_objects:
idx = self.lines.index(o)
FreeCAD.Console.PrintMessage("Subdividing line #{}\n".format(idx))
p1 = o.markers[0].points[0]
p2 = o.markers[1].points[0]
par1 = self.curve.parameter(FreeCAD.Vector(p1))
par2 = self.curve.parameter(FreeCAD.Vector(p2))
midpar = (par1 + par2) / 2.0
mark = MarkerOnShape([self.curve.value(midpar)])
pts.append(mark)
new_select.append(mark)
pts.append(self.points[-1])
self.points = pts
self.setup_InteractionSeparator()
self.root.selected_objects = new_select
self.update_curve()
return(True)
def quit(self):
self.root.events.removeEventCallback(coin.SoKeyboardEvent.getClassTypeId(), self._controlCB)
self.root.unregister()
# self.root.removeAllChildren()
self.sg.removeChild(self.root)
self.root_inserted = False
def get_guide_params():
sel = FreeCADGui.Selection.getSelectionEx()
pts = list()
for s in sel:
pts.extend(list(zip(s.PickedPoints, s.SubObjects)))
return(pts)
def main():
obj = FreeCAD.ActiveDocument.addObject("Part::Spline", "profile")
tups = get_guide_params()
InterpoCurveEditor(tups, obj)
FreeCAD.ActiveDocument.recompute()
if __name__ == '__main__':
main()
+2 -2
View File
@@ -1,8 +1,8 @@
# Script para FreeCAD - Procesador de Documentos Word con Carátula
import os
import glob
from PySide2 import QtWidgets, QtCore
from PySide2.QtWidgets import (QFileDialog, QMessageBox, QProgressDialog,
from PySide import QtWidgets, QtCore
from PySide.QtWidgets import (QFileDialog, QMessageBox, QProgressDialog,
QApplication, QVBoxLayout, QWidget, QPushButton,
QLabel, QTextEdit)
import FreeCAD
+1 -1
View File
@@ -159,7 +159,7 @@ def calculate_incenter(facet):
"""Calcula el incentro usando la función nativa de FreeCAD"""
try:
return facet.InCircle[0] # (x, y, z)
except:
except (IndexError, AttributeError):
return None
-139
View File
@@ -1,139 +0,0 @@
# /**********************************************************************
# * *
# * Copyright (c) 2026 Javier Brana <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 *
# * *
# ***********************************************************************
"""
Proyecciones y transformaciones geodésicas unificadas para PVPlant.
Reemplaza el uso disperso de la librería 'utm' con pyproj (PROJ),
soporte multi-zona UTM y transformaciones entre datums.
Uso básico:
from lib.projection import latlon_to_utm, utm_to_latlon, get_utm_zone
"""
import FreeCAD
from pyproj import CRS, Transformer
from pyproj.aoi import AreaOfInterest
from pyproj.database import query_utm_crs_info
# WGS84 sistema de coordenadas geográfico de referencia
_WGS84 = CRS.from_epsg(4326)
# Cache de transformadores UTM por zona (lazy)
_utm_transformers = {}
def _get_utm_transformer(lat, lon):
"""Obtiene (o crea en caché) un transformador UTM para la zona de las coordenadas dadas.
Returns:
tuple: (transformer, zone_number, zone_letter)
"""
# Determinar la zona UTM a partir de lat/lon
zone_number = int((lon + 180) / 6) + 1
if lat >= 0:
zone_letter = 'N'
epsg = 32600 + zone_number
else:
zone_letter = 'S'
epsg = 32700 + zone_number
cache_key = (zone_number, zone_letter)
if cache_key not in _utm_transformers:
utm_crs = CRS.from_epsg(epsg)
_utm_transformers[cache_key] = Transformer.from_crs(
_WGS84, utm_crs, always_xy=True
)
return _utm_transformers[cache_key], zone_number, zone_letter
def latlon_to_utm(lat, lon):
"""Convierte coordenadas geográficas (WGS84) a UTM (este, norte, zona, letra).
Args:
lat (float): Latitud en grados.
lon (float): Longitud en grados.
Returns:
tuple: (easting, northing, zone_number, zone_letter)
easting/northing en metros.
"""
transformer, zone_number, zone_letter = _get_utm_transformer(lat, lon)
easting, northing = transformer.transform(lon, lat)
return easting, northing, zone_number, zone_letter
def utm_to_latlon(easting, northing, zone_number, zone_letter='N'):
"""Convierte coordenadas UTM a geográficas (WGS84).
Args:
easting (float): Coordenada E en metros.
northing (float): Coordenada N en metros.
zone_number (int): Número de zona UTM (1-60).
zone_letter (str): Letra de zona ('N' o 'S').
Returns:
tuple: (latitude, longitude) en grados.
"""
if zone_letter.upper() not in ('N', 'S'):
zone_letter = 'N'
epsg = 32600 + zone_number if zone_letter.upper() == 'N' else 32700 + zone_number
utm_crs = CRS.from_epsg(epsg)
transformer = Transformer.from_crs(utm_crs, _WGS84, always_xy=True)
lon, lat = transformer.transform(easting, northing)
return lat, lon
def get_utm_zone(lat, lon):
"""Obtiene la zona UTM para unas coordenadas dadas.
Args:
lat (float): Latitud en grados.
lon (float): Longitud en grados.
Returns:
tuple: (zone_number, zone_letter)
"""
_, _, zone_number, zone_letter = latlon_to_utm(lat, lon)
return zone_number, zone_letter
def latlon_to_utm_vector(lat, lon, elevation=0.0):
"""Convierte lat/lon/elevación a un FreeCAD.Vector en UTM (mm).
Args:
lat (float): Latitud en grados.
lon (float): Longitud en grados.
elevation (float): Elevación en metros (default 0).
Returns:
FreeCAD.Vector: Coordenadas UTM en milímetros.
"""
transformer, _, _ = _get_utm_transformer(lat, lon)
easting, northing = transformer.transform(lon, lat)
return FreeCAD.Vector(
round(easting, 0),
round(northing, 0),
round(elevation, 0)
) * 1000
+1 -1
View File
@@ -2,7 +2,6 @@ numpy~=1.26.2
opencv-python~=4.8.1
matplotlib~=3.8.2
openpyxl~=3.1.2
PySide2~=5.15.8
requests~=2.31.0
setuptools~=68.2.2
laspy~=2.5.3
@@ -17,3 +16,4 @@ SciPy~=1.11.4
pycollada~=0.7.2
shapely
rtree
pandas
+1 -1
View File
@@ -22,7 +22,7 @@
import FreeCAD, FreeCADGui
#from freecad.trails import ICONPATH
from PySide2.QtWidgets import QLabel
from PySide.QtWidgets import QLabel
import copy