Отрисовка блоков
This commit is contained in:
parent
17ca479190
commit
8ade6935c7
@ -660,6 +660,174 @@
|
||||
}
|
||||
});
|
||||
|
||||
function processBlockInsert(entity, dxf) {
|
||||
const blockName = entity.name;
|
||||
const block = dxf.blocks[blockName];
|
||||
|
||||
if (!block) {
|
||||
console.warn(`Block ${blockName} not found`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const features = [];
|
||||
const x = entity.position.x;
|
||||
const y = entity.position.y;
|
||||
const rotation = entity.rotation || 0;
|
||||
const scale = entity.scale || {x: 1, y: 1, z: 1};
|
||||
|
||||
// Обрабатываем каждую сущность в блоке
|
||||
block.entities.forEach(blockEntity => {
|
||||
// Копируем сущность, чтобы не модифицировать оригинал
|
||||
const entityCopy = JSON.parse(JSON.stringify(blockEntity));
|
||||
let feature = null;
|
||||
let coords = [];
|
||||
|
||||
// Устанавливаем слой из INSERT, если он не наследуется
|
||||
if (!entityCopy.layer || entityCopy.layer === '0') {
|
||||
entityCopy.layer = entity.layer;
|
||||
}
|
||||
|
||||
// Обрабатываем каждый тип сущности и сразу создаем фичу
|
||||
if (entityCopy.type === 'CIRCLE') {
|
||||
// Применяем трансформации
|
||||
entityCopy.radius *= scale.x;
|
||||
const centerX = x + (entityCopy.center.x * scale.x);
|
||||
const centerY = y + (entityCopy.center.y * scale.y);
|
||||
|
||||
// Создаем фичу
|
||||
const normalizedCoords = convertToLonLat([centerX, centerY]);
|
||||
feature = createFeature('Point', normalizedCoords, {
|
||||
style: getStyleFromDXFEntity(entityCopy, dxf.tables.lineType?.lineTypes),
|
||||
layer: entityCopy.layer
|
||||
});
|
||||
} else if (entityCopy.type === 'LINE') {
|
||||
// Применяем трансформации и создаем координаты
|
||||
coords = entityCopy.vertices.map(vertex => {
|
||||
let vx = vertex.x * scale.x;
|
||||
let vy = vertex.y * scale.y;
|
||||
|
||||
if (rotation !== 0) {
|
||||
const angle = (rotation * Math.PI) / 180;
|
||||
const cos = Math.cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
vx = vx * cos - vy * sin;
|
||||
vy = vx * sin + vy * cos;
|
||||
}
|
||||
|
||||
return [x + vx, y + vy];
|
||||
});
|
||||
|
||||
// Создаем фичу
|
||||
const normalizedCoords = coords.map(coord => convertToLonLat(coord));
|
||||
feature = createFeature('LineString', normalizedCoords, {
|
||||
style: getStyleFromDXFEntity(entityCopy, dxf.tables.lineType?.lineTypes),
|
||||
layer: entityCopy.layer
|
||||
});
|
||||
} else if (entityCopy.type === 'LWPOLYLINE' || entityCopy.type === 'POLYLINE') {
|
||||
// Применяем трансформации и создаем координаты
|
||||
coords = entityCopy.vertices.map(vertex => {
|
||||
let vx = vertex.x * scale.x;
|
||||
let vy = vertex.y * scale.y;
|
||||
|
||||
if (rotation !== 0) {
|
||||
const angle = (rotation * Math.PI) / 180;
|
||||
const cos = Math.cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
vx = vx * cos - vy * sin;
|
||||
vy = vx * sin + vy * cos;
|
||||
}
|
||||
|
||||
return [x + vx, y + vy];
|
||||
});
|
||||
|
||||
// Создаем фичу
|
||||
const normalizedCoords = coords.map(coord => convertToLonLat(coord));
|
||||
feature = createFeature(
|
||||
entityCopy.closed ? 'Polygon' : 'LineString',
|
||||
entityCopy.closed ? [normalizedCoords] : normalizedCoords,
|
||||
{
|
||||
style: getStyleFromDXFEntity(entityCopy, dxf.tables.lineType?.lineTypes),
|
||||
layer: entityCopy.layer
|
||||
}
|
||||
);
|
||||
} else if (entityCopy.type === 'ARC') {
|
||||
// Применяем трансформации
|
||||
const centerX = x + (entityCopy.center.x * scale.x);
|
||||
const centerY = y + (entityCopy.center.y * scale.y);
|
||||
entityCopy.radius *= scale.x;
|
||||
entityCopy.center = {x: centerX, y: centerY};
|
||||
|
||||
if (rotation !== 0) {
|
||||
entityCopy.startAngle += rotation;
|
||||
entityCopy.endAngle += rotation;
|
||||
}
|
||||
|
||||
// Создаем фичу
|
||||
const arcCoords = arcToLine(entityCopy);
|
||||
const normalizedCoords = arcCoords.map(coord => convertToLonLat(coord));
|
||||
feature = createFeature('LineString', normalizedCoords, {
|
||||
style: getStyleFromDXFEntity(entityCopy, dxf.tables.lineType?.lineTypes),
|
||||
layer: entityCopy.layer
|
||||
});
|
||||
} else if (entityCopy.type === 'POINT') {
|
||||
// Применяем трансформации
|
||||
const pointX = x + (entityCopy.position.x * scale.x);
|
||||
const pointY = y + (entityCopy.position.y * scale.y);
|
||||
|
||||
// Создаем фичу
|
||||
const normalizedCoords = convertToLonLat([pointX, pointY]);
|
||||
feature = createFeature('Point', normalizedCoords, {
|
||||
style: getStyleFromDXFEntity(entityCopy, dxf.tables.lineType?.lineTypes),
|
||||
layer: entityCopy.layer
|
||||
});
|
||||
} else if (entityCopy.type === 'SPLINE') {
|
||||
// Применяем трансформации и создаем координаты
|
||||
coords = entityCopy.controlPoints.map(point => {
|
||||
let px = point.x * scale.x;
|
||||
let py = point.y * scale.y;
|
||||
|
||||
if (rotation !== 0) {
|
||||
const angle = (rotation * Math.PI) / 180;
|
||||
const cos = Math.cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
px = px * cos - py * sin;
|
||||
py = px * sin + py * cos;
|
||||
}
|
||||
|
||||
return [x + px, y + py];
|
||||
});
|
||||
|
||||
// Создаем фичу
|
||||
const normalizedCoords = coords.map(coord => convertToLonLat(coord));
|
||||
feature = createFeature('LineString', normalizedCoords, {
|
||||
style: getStyleFromDXFEntity(entityCopy, dxf.tables.lineType?.lineTypes),
|
||||
layer: entityCopy.layer
|
||||
});
|
||||
} else if (entityCopy.type === 'TEXT' || entityCopy.type === 'MTEXT') {
|
||||
// Применяем трансформации
|
||||
const textX = x + (entityCopy.position.x * scale.x);
|
||||
const textY = y + (entityCopy.position.y * scale.y);
|
||||
|
||||
if (rotation !== 0) {
|
||||
entityCopy.rotation = (entityCopy.rotation || 0) + rotation;
|
||||
}
|
||||
|
||||
// Создаем фичу
|
||||
const normalizedCoords = convertToLonLat([textX, textY]);
|
||||
feature = createFeature('Point', normalizedCoords, {
|
||||
style: getStyleFromDXFEntity(entityCopy),
|
||||
layer: entityCopy.layer
|
||||
});
|
||||
}
|
||||
|
||||
if (feature) {
|
||||
features.push(feature);
|
||||
}
|
||||
});
|
||||
|
||||
return features;
|
||||
}
|
||||
|
||||
function getCoordinates(entity) {
|
||||
if (entity.type === 'LINE') {
|
||||
return [[entity.vertices[0].x, entity.vertices[0].y], [entity.vertices[1].x, entity.vertices[1].y]];
|
||||
@ -717,80 +885,106 @@
|
||||
}
|
||||
|
||||
function dxfToGeoJSON(dxf) {
|
||||
console.log(dxf);
|
||||
const normalizedEntities = normalizeCoordinates(dxf.entities, dxf.header);
|
||||
const features = normalizedEntities
|
||||
.map(entity => {
|
||||
DXFLayersList.push(entity.layer)
|
||||
if (entity.type === 'LINE') {
|
||||
return createFeature('LineString',
|
||||
entity.normalizedCoords,
|
||||
{
|
||||
style: {
|
||||
"stroke-color": getColorByColorTable(entity.colorIndex), "stroke-width": 1,
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
} else if (entity.type === 'LWPOLYLINE' || entity.type === 'POLYLINE') {
|
||||
const coords = entity.normalizedCoords;
|
||||
return createFeature(entity.closed ? 'Polygon' : 'LineString',
|
||||
entity.closed ? [coords] : coords,
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
} else if (entity.type === 'TEXT' || entity.type === 'MTEXT') {
|
||||
return createFeature('Point',
|
||||
entity.normalizedCoords[0],
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
} else if (entity.type === 'CIRCLE') {
|
||||
return createFeature('Point',
|
||||
entity.normalizedCoords[0],
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
} else if (entity.type === 'ARC') {
|
||||
return createFeature('LineString',
|
||||
entity.normalizedCoords,
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
} else if (entity.type === 'POINT') {
|
||||
return createFeature('Point',
|
||||
entity.normalizedCoords[0],
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
} else if (entity.type === 'SPLINE') {
|
||||
return createFeature('LineString',
|
||||
entity.normalizedCoords,
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
}
|
||||
return null;
|
||||
}).filter(f => f !== null);
|
||||
return {type: 'FeatureCollection', features: features};
|
||||
console.log(dxf);
|
||||
const normalizedEntities = normalizeCoordinates(dxf.entities, dxf.header);
|
||||
let features = [];
|
||||
|
||||
normalizedEntities.forEach(entity => {
|
||||
DXFLayersList.push(entity.layer);
|
||||
|
||||
if (entity.type === 'INSERT') {
|
||||
// Обрабатываем блоки
|
||||
const blockFeatures = processBlockInsert(entity, dxf);
|
||||
console.log(blockFeatures);
|
||||
if (blockFeatures && blockFeatures.length > 0) {
|
||||
features.push(...blockFeatures);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Обрабатываем обычные сущности
|
||||
let feature = null;
|
||||
|
||||
if (entity.type === 'LINE') {
|
||||
feature = createFeature('LineString',
|
||||
entity.normalizedCoords,
|
||||
{
|
||||
style: {
|
||||
"stroke-color": getColorByColorTable(entity.colorIndex),
|
||||
"stroke-width": 1,
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
}
|
||||
else if (entity.type === 'LWPOLYLINE' || entity.type === 'POLYLINE') {
|
||||
const coords = entity.normalizedCoords;
|
||||
feature = createFeature(entity.closed ? 'Polygon' : 'LineString',
|
||||
entity.closed ? [coords] : coords,
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType?.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
}
|
||||
else if (entity.type === 'TEXT' || entity.type === 'MTEXT') {
|
||||
feature = createFeature('Point',
|
||||
entity.normalizedCoords[0],
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
}
|
||||
else if (entity.type === 'CIRCLE') {
|
||||
feature = createFeature('Point',
|
||||
entity.normalizedCoords[0],
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType?.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
}
|
||||
else if (entity.type === 'ARC') {
|
||||
feature = createFeature('LineString',
|
||||
entity.normalizedCoords,
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType?.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
}
|
||||
else if (entity.type === 'POINT') {
|
||||
feature = createFeature('Point',
|
||||
entity.normalizedCoords[0],
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType?.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
}
|
||||
else if (entity.type === 'SPLINE') {
|
||||
feature = createFeature('LineString',
|
||||
entity.normalizedCoords,
|
||||
{
|
||||
style: {
|
||||
...getStyleFromDXFEntity(entity, dxf.tables.lineType?.lineTypes)
|
||||
},
|
||||
"layer": entity.layer
|
||||
});
|
||||
}
|
||||
|
||||
if (feature) {
|
||||
features.push(feature);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return {type: 'FeatureCollection', features: features};
|
||||
}
|
||||
|
||||
function createFeature(type, coordinates, properties) {
|
||||
i++
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user