refactor(engine): migrate clipping to unified activeSection API
- Replace old state variables with currentSectionMode - Implement unified activeSection(mode) for all clipping types (x/y/z/box/face) - Remove deprecated methods: activateSectionAxis, activateSectionBox, etc. - Use new underlying API: engine.clipping.active(mode) - Code reduction: ~175 lines → ~38 lines (78% reduction)
This commit is contained in:
@@ -44,10 +44,8 @@ export class Engine implements IBimComponent {
|
||||
private currentMeasureType: MeasureMode | null = null;
|
||||
/** 主测量功能是否已激活 */
|
||||
private isMeasureActive: boolean = false;
|
||||
/** 当前激活的剖切轴向 */
|
||||
private currentSectionAxis: 'x' | 'y' | 'z' | null = null;
|
||||
/** 剖切盒是否激活 */
|
||||
private isSectionBoxActive: boolean = false;
|
||||
/** 当前激活的剖切模式 */
|
||||
private currentSectionMode: 'x' | 'y' | 'z' | 'box' | 'face' | null = null;
|
||||
/** 当前选中的构件信息 */
|
||||
private selectedComponent: { url: string; id: string } | null = null;
|
||||
|
||||
@@ -425,182 +423,45 @@ export class Engine implements IBimComponent {
|
||||
|
||||
// ==================== 结束:测量功能方法 ====================
|
||||
|
||||
// ==================== 轴向剖切功能 ====================
|
||||
// ==================== 剖切功能(统一 API) ====================
|
||||
|
||||
/**
|
||||
* 激活轴向剖切
|
||||
* @param axis 要激活的轴向 ('x' | 'y' | 'z')
|
||||
* @remarks
|
||||
* - 如果传入的轴向与当前激活的轴向相同,则静默返回(幂等操作)
|
||||
* - 如果当前有不同的轴向激活,先调用该轴向的 disActive() 再激活新轴向
|
||||
* - 使用单个 plane 的 disActive() 而非 clipping.disActive(),避免影响其他剖切功能
|
||||
* - 如果引擎未初始化或 clipping 模块不可用,方法会静默返回并输出错误日志
|
||||
*/
|
||||
public activateSectionAxis(axis: 'x' | 'y' | 'z'): void {
|
||||
// 1. 检查引擎初始化
|
||||
if (!this._isInitialized || !this.engine) {
|
||||
console.error('[Engine] Cannot activate section axis: engine not initialized.');
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 检查 clipping 模块
|
||||
if (!this.engine.clipping) {
|
||||
console.error('[Engine] Clipping module not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 如果是同一轴向,静默返回(幂等操作)
|
||||
if (this.currentSectionAxis === axis) {
|
||||
console.log(`[Engine] Section axis ${axis} already active, skipping.`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 如果当前有激活的轴向且不同,先停用当前轴向
|
||||
if (this.currentSectionAxis) {
|
||||
this.deactivateCurrentSectionAxis();
|
||||
}
|
||||
|
||||
// 5. 激活新轴向
|
||||
const planeMap: Record<'x' | 'y' | 'z', any> = {
|
||||
'x': this.engine.clipping.sectionPlaneX,
|
||||
'y': this.engine.clipping.sectionPlaneY,
|
||||
'z': this.engine.clipping.sectionPlaneZ
|
||||
};
|
||||
const plane = planeMap[axis];
|
||||
|
||||
if (plane && typeof plane.active === 'function') {
|
||||
console.log(`[Engine] Activating section axis: ${axis}`);
|
||||
plane.active();
|
||||
this.currentSectionAxis = axis;
|
||||
} else {
|
||||
console.error(`[Engine] Section plane ${axis} not available.`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用当前轴向剖切(内部方法)
|
||||
* @remarks 只停用当前激活的单个轴向,不影响其他剖切功能
|
||||
*/
|
||||
private deactivateCurrentSectionAxis(): void {
|
||||
if (!this.currentSectionAxis || !this.engine?.clipping) {
|
||||
return;
|
||||
}
|
||||
|
||||
const planeMap: Record<'x' | 'y' | 'z', any> = {
|
||||
'x': this.engine.clipping.sectionPlaneX,
|
||||
'y': this.engine.clipping.sectionPlaneY,
|
||||
'z': this.engine.clipping.sectionPlaneZ
|
||||
};
|
||||
const plane = planeMap[this.currentSectionAxis];
|
||||
|
||||
if (plane && typeof plane.disActive === 'function') {
|
||||
console.log(`[Engine] Deactivating section axis: ${this.currentSectionAxis}`);
|
||||
plane.disActive();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用轴向剖切(公共方法,关闭弹窗时调用)
|
||||
* @remarks 使用 clipping.disActive() 停用所有剖切,清理状态
|
||||
*/
|
||||
public deactivateSectionAxis(): void {
|
||||
public activeSection(mode: 'x' | 'y' | 'z' | 'box' | 'face'): void {
|
||||
if (!this._isInitialized || !this.engine?.clipping) {
|
||||
console.error('[Engine] Cannot activate section: engine not initialized.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.currentSectionAxis) {
|
||||
console.log('[Engine] Deactivating all section axis');
|
||||
this.engine.clipping.disActive();
|
||||
this.currentSectionAxis = null;
|
||||
if (this.currentSectionMode === mode) {
|
||||
console.log(`[Engine] Section ${mode} already active, skipping.`);
|
||||
return;
|
||||
}
|
||||
console.log(`[Engine] Activating section: ${mode}`);
|
||||
this.engine.clipping.active(mode);
|
||||
this.currentSectionMode = mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前剖切轴向
|
||||
* @returns 当前激活的轴向,如果未激活则返回 null
|
||||
*/
|
||||
public getCurrentSectionAxis(): 'x' | 'y' | 'z' | null {
|
||||
return this.currentSectionAxis;
|
||||
public getCurrentSectionMode(): 'x' | 'y' | 'z' | 'box' | 'face' | null {
|
||||
return this.currentSectionMode;
|
||||
}
|
||||
|
||||
// ==================== 结束:轴向剖切功能 ====================
|
||||
|
||||
// ==================== 剖切盒功能 ====================
|
||||
|
||||
/** 激活剖切盒 */
|
||||
public activateSectionBox(): void {
|
||||
if (!this._isInitialized || !this.engine) {
|
||||
console.error('[Engine] Cannot activate section box: engine not initialized.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.engine.clipping?.sectionBox) {
|
||||
console.error('[Engine] Section box module not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isSectionBoxActive) {
|
||||
console.log('[Engine] Section box already active, skipping.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[Engine] Activating section box');
|
||||
this.engine.clipping.sectionBox.active();
|
||||
this.isSectionBoxActive = true;
|
||||
}
|
||||
|
||||
/** 停用剖切盒 */
|
||||
public deactivateSectionBox(): void {
|
||||
if (!this._isInitialized || !this.engine?.clipping?.sectionBox) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.isSectionBoxActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[Engine] Deactivating section box');
|
||||
this.engine.clipping.sectionBox.disActive();
|
||||
this.isSectionBoxActive = false;
|
||||
}
|
||||
|
||||
/** 设置剖切盒范围(百分比 0-100) */
|
||||
public setSectionBoxRange(range: SectionBoxRange): void {
|
||||
if (!this._isInitialized || !this.engine?.clipping?.sectionBox) {
|
||||
if (!this._isInitialized || !this.engine?.clipping) {
|
||||
console.error('[Engine] Cannot set section box range: engine not initialized.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[Engine] Setting section box range:', range);
|
||||
this.engine.clipping.sectionBox.setboxPercent(range);
|
||||
this.engine.clipping.updateClippingValue(range);
|
||||
}
|
||||
|
||||
/** 适应剖切盒到模型(将范围设置为整个模型包围盒) */
|
||||
public fitSectionBoxToModel(): void {
|
||||
if (!this._isInitialized || !this.engine?.clipping?.sectionBox) {
|
||||
console.error('[Engine] Cannot fit section box: engine not initialized.');
|
||||
public deactivateSection(): void {
|
||||
if (!this._isInitialized || !this.engine?.clipping) {
|
||||
return;
|
||||
}
|
||||
|
||||
const box = this.engine.octreeBox?.getBoundingBox();
|
||||
if (!box) {
|
||||
console.error('[Engine] Cannot fit section box: model bounding box not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[Engine] Fitting section box to model');
|
||||
this.engine.clipping.sectionBox.setBox(box);
|
||||
console.log('[Engine] Deactivating all sections');
|
||||
this.engine.clipping.disActive();
|
||||
this.currentSectionMode = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置剖切盒
|
||||
* @remarks 将剖切盒范围恢复为整个模型的包围盒
|
||||
*/
|
||||
public resetSectionBox(): void {
|
||||
this.fitSectionBoxToModel();
|
||||
}
|
||||
|
||||
// ==================== 结束:剖切盒功能 ====================
|
||||
// ==================== 结束:剖切功能 ====================
|
||||
|
||||
// ==================== 漫游功能 ====================
|
||||
|
||||
|
||||
Reference in New Issue
Block a user