diff --git a/src/parser/DxfParser.ts b/src/parser/DxfParser.ts index 2570aad..d866e23 100644 --- a/src/parser/DxfParser.ts +++ b/src/parser/DxfParser.ts @@ -5,7 +5,6 @@ import { parseBlocks } from "./blocks"; import { parseEntities } from "./entities"; import { parseObjects } from "./objects"; import { isMatched } from "./shared"; -import { filterDummyBlocks } from "./filterDummyBlocks"; import type { ParsedDxf } from "./types"; import type { Readable } from "readable-stream"; @@ -120,6 +119,6 @@ export class DxfParser extends EventTarget { } curr = scanner.next(); } - return filterDummyBlocks(dxf); + return dxf; } } diff --git a/src/parser/filterDummyBlocks.ts b/src/parser/filterDummyBlocks.ts deleted file mode 100644 index 1b37a5c..0000000 --- a/src/parser/filterDummyBlocks.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { filter, map } from '@fxts/core'; -import type { ParsedDxf } from './types'; -import type { CommonDxfEntity } from './entities/shared'; -import type { DimensionEntity } from './entities/dimension/types'; -import type { InsertEntity } from './entities/insert'; -import { flooding } from '../utils'; - -export function filterDummyBlocks(dxf: ParsedDxf): ParsedDxf { - // INSERT/DIMENSION 엔티티가 포함된 블록으로 그래프를 만듦 - const namedBlocks = Object.entries(dxf.blocks); - const graph: Record = {}; - const insertBlocks = filter( - ([_, block]) => block.entities?.some(filterInsertOrDimension), - namedBlocks, - ); - - for (const [name, block] of insertBlocks) { - for (const subEntity of block.entities ?? []) { - if (!filterInsertOrDimension(subEntity)) continue; - - graph[name] ??= []; - graph[name].push(subEntity.name); - } - } - - // 루트 INSERT/DIMENSION 엔티티에서 접근가능한 모든 블록 이름을 수집 - const insertEntities = filter(filterInsertOrDimension, dxf.entities); - const reachableNames = flooding({ - seeds: [...map(({ name }) => name, insertEntities)], - serializer: (name) => name, - spanner: (name) => graph[name] ?? [], - }).flat(); - - return { - ...dxf, - blocks: Object.fromEntries( - reachableNames.map((name) => [name, dxf.blocks[name]]), - ), - }; -} - -function filterInsertOrDimension( - entity: CommonDxfEntity, -): entity is InsertEntity | DimensionEntity { - return ( - entity.type === 'INSERT' || - (entity.type === 'DIMENSION' && !!(entity as DimensionEntity).name) - ); -} diff --git a/src/utils/flooding.ts b/src/utils/flooding.ts deleted file mode 100644 index d8541b1..0000000 --- a/src/utils/flooding.ts +++ /dev/null @@ -1,37 +0,0 @@ -interface FloodingParams { - seeds: T[]; - spanner(seed: T): T[]; - serializer(seed: T): number | string; - iterationLimit?: number; -} - -export function flooding({ - seeds, - spanner, - serializer, - iterationLimit = Infinity, -}: FloodingParams) { - const memo = new Set(); - let iterations = 0; - - return seeds - .map((seed) => { - const group: T[] = []; - const stack: [T, string | number][] = [[seed, serializer(seed)]]; - - while (stack.length && iterations++ < iterationLimit) { - const [current, key] = stack.pop()!; - - if (memo.has(key)) continue; - memo.add(key); - group.push(current); - - for (const next of spanner(current)) { - stack.push([next, serializer(next)]); - } - } - - return group; - }) - .filter((group) => group.length); -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 805c876..e5da6c3 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,3 @@ -export * from './flooding'; - import type { Point2D, Point3D } from '@src/types' export function classify(