Get wire/edge position by absolute distance. (Convert Face to GeoJson)
- Lingua principale
- Python
- Stelle
- 5.8k
- Fork
- 541
- Merge medio
- 3g 2h
- PR unite (30g)
- 5
Descrizione
I found that if I export faces to dxf, the end points wouldn't be conneted.
Such like following :

So I try to convert the face element to GeoJson.
```PYTHON
import shapely
from shapely.geometry import *
import cadquery as cq
from cadquery.selectors import*
def wire_to_Polygon(wire, interval_distance):
wire_length = wire.Length()
if interval_distance<10e-6:
interval_distance=10e-6
elif interval_distance > wire_length/100:
interval_distance = wire_length/100
div = wire_length/interval_distance
linspace = [ i/div for i in range(int(div))]+[1]
vector_list = wire.positions(linspace,mode='length')
linestring = LineString([ (vector.x,vector.y) for vector in vector_list])
linestring_boundarys = linestring.buffer(interval_distance).buffer(-1*interval_distance*(0.99)).boundary
if isinstance( linestring_boundarys,type(LineString()) ):
if interval_distance == 10e-6:
print('In wire_to_Polygon : Have the aspect ratio is too large. '+'Change interval_distance = '+str(interval_distance))
linestring = linestring_boundarys
else:
print('In wire_to_Polygon : Have the aspect ratio is too large. '+'Change interval_distance = '+str(interval_distance/2))
linestring = wire_to_Polygon(wire, interval_distance/2).boundary
else:
linestring = linestring_boundarys[0]
return Polygon(linestring)
def face_to_polygon(face, interval_distance):
# Only read X and Y coordinate.
outerWire = face.outerWire()
innerWires = face.innerWires()
polygon = Polygon(wire_to_Polygon(outerWire, interval_distance))
for innerWire in innerWires:
polygon = polygon.difference(wire_to_Polygon(innerWire, interval_distance))
return polygon
def cut_section_to_geojson(result, Plane, interval_distance):
result_plane = cq.Workplane(Plane)
result_plane.objects = result.vals()
result_section = result_plane.section()
section_faces = [result_section.plane.toLocalCoords(face_) for object_ in result_section.objects for face_ in object_.Faces()]
polygons = [ face_to_polygon(face,interval_distance) for face in section_faces]
return polygons
```
But I have a problem about the function `wire.positions()`. It use `wire.paramAt()` to get parameter value, and the `param d` in `wire.paramAt()` is normalized distance `[0, 1]` not absolute distance.
I think it should add absolute distance to get position. Like this:
```python
Mixin1D.positions([0, 0.2, 0.9, 1.2], mode = "absolute distance")
```
Besides this, you can try my function to get the closed and connected polygon with GeoJson.
```python
result0 = cq.Workplane("XY")
result0 = result0.moveTo(3,4).circle(1).extrude(10,both=True)
result0 = result0.spline([(0,0),(1,0),(1,1),(0,1)]).close().extrude(1,both=True).translate((-3,-4,0))
result0 = result0.moveTo(4,3).circle(1).extrude(1,both=True)
result0 = result0.union( cq.Workplane("XY").
spline([(0,0),(-1,0),(-0.1,-0.5),(-1,-1),(0,-1)]).
close().
extrude(1,both=True).
translate((3,-4,0.5)).
rotate((0,0,0),(1,0,0),(5))
)
result0.plane = cq.Plane(origin=(0,0.1,0.5), normal=(0.05,0.05,1))
#result0 = result0.section()
polygons = cut_section_to_geojson(result0,result0.plane,0.1)
MultiPolygon(polygons)
```

And it would be easy to use ezdxf to convet GeoJson to dxf.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia da Mixin1D.positions e dalla chiamata wire.paramAt descritta nell’issue, quindi traccia il modo in cui wire.positions passa valori normalizzati in modalità lunghezza. Definisci il comportamento previsto per le distanze assolute, inclusi i valori di esempio e le distanze oltre la lunghezza del wire, e verifica le posizioni risultanti rispetto all’esempio di conversione GeoJson.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- computer-graphics
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100