From 42932dd8b74156811800d070db6966004abe409b Mon Sep 17 00:00:00 2001 From: Stanislav Permiakov Date: Mon, 13 Feb 2023 15:33:10 +0300 Subject: [PATCH] #1990 - Detect if group is attached to smth + get attachment atom --- .../ketcher-core/src/domain/entities/sgroup.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/ketcher-core/src/domain/entities/sgroup.ts b/packages/ketcher-core/src/domain/entities/sgroup.ts index 7d3d90ecee..c826b5d2f6 100644 --- a/packages/ketcher-core/src/domain/entities/sgroup.ts +++ b/packages/ketcher-core/src/domain/entities/sgroup.ts @@ -214,6 +214,23 @@ export class SGroup { this.pp = topLeftPoint } + getAttAtomId(struct: Struct): number { + for (const atomId of this.atoms) { + const atom = struct.atoms.get(atomId) + if (!atom) continue + if (Number.isInteger(atom.attpnt)) return atomId + } + // in normal circumstances this should never be invoked + return this.atoms[0] + } + + isGroupAttached(struct: Struct): boolean { + const attachPointId = this.getAttAtomId(struct) + const neighbours = struct.atomGetNeighbors(attachPointId) + + return !!neighbours?.every(({ aid }) => this.atoms.includes(aid)) + } + static getOffset(sgroup: SGroup): null | Vec2 { if (!sgroup?.pp) return null return Vec2.diff(sgroup.pp, sgroup.bracketBox.p1)