Skip to content

Commit

Permalink
#225 – moved CIP rendering logic to Atom class
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitvex committed Mar 20, 2023
1 parent bf2eaf9 commit c608227
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
53 changes: 52 additions & 1 deletion packages/ketcher-core/src/application/render/restruct/reatom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ReAtom extends ReObject {

show(restruct: ReStruct, aid: number, options: any): void {
// eslint-disable-line max-statements
const atom = restruct.molecule.atoms.get(aid)
const atom = restruct.molecule.atoms.get(aid)!
const sgroups = restruct.molecule.sgroups
const functionalGroups = restruct.molecule.functionalGroups
const render = restruct.render
Expand Down Expand Up @@ -402,7 +402,58 @@ class ReAtom extends ReObject {

restruct.addReObjectPath(LayerMap.hovering, this.visel, path)
}

if (atom.cip) {
this.drawCIPLabel(atom, render.ctab)
}
}

drawCIPLabel(atom: Atom, restruct: ReStruct) {
const render = restruct.render
const { options, paper } = render
const path = paper.set()

const cipLabelPosition = atom.pp.scaled(options.scale)
const cipValuePath = getCIPValuePath({
paper,
cipLabelPosition,
atom,
options
})
const box = util.relBox(cipValuePath.getBBox())

cipValuePath.translateAbs(0.5 * box.width, -0.5 * box.height)
path.push(cipValuePath.toBack())

restruct.addReObjectPath(LayerMap.data, this.visel, path, null, true)
}
}

function getCIPValuePath({
paper,
cipLabelPosition,
atom,
options
}: {
paper
cipLabelPosition: Vec2
atom: Atom
options
}) {
const text = paper
.text(cipLabelPosition.x, cipLabelPosition.y, `(${atom.cip})`)
.attr({
font: options.font,
'font-size': options.fontsz
})
const box = text.getBBox()
const path = paper.set()
const rect = paper
.rect(box.x - 1, box.y - 1, box.width + 2, box.height + 2, 3, 3)
.attr({ fill: '#0f0', stroke: '#fff', opacity: 1 })
path.push(rect.toBack(), text.toBack())

return path
}

function getStereoAtomColor(options, stereoLabel) {
Expand Down
11 changes: 11 additions & 0 deletions packages/ketcher-core/src/domain/entities/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export enum StereoLabel {
Or = 'or'
}

export enum CIP {
S = 'S',
R = 'R',
s = 's',
r = 'r'
}

export interface AtomAttributes {
stereoParity?: number
stereoLabel?: string | null
Expand All @@ -69,6 +76,7 @@ export interface AtomAttributes {
rglabel?: string | null
charge?: number
radical?: number
cip?: CIP | null
isotope?: number
alias?: string | null
pseudo?: string
Expand Down Expand Up @@ -101,6 +109,7 @@ export class Atom {
label: 'C',
isotope: 0,
radical: 0,
cip: null,
charge: 0,
explicitValence: -1,
ringBondCount: 0,
Expand All @@ -125,6 +134,7 @@ export class Atom {
isotope: number
hCount: number
radical: number
cip: CIP | null
charge: number
explicitValence: number
ringBondCount: number
Expand Down Expand Up @@ -153,6 +163,7 @@ export class Atom {
this.alias = getValueOrDefault(attributes.alias, Atom.attrlist.alias)
this.isotope = getValueOrDefault(attributes.isotope, Atom.attrlist.isotope)
this.radical = getValueOrDefault(attributes.radical, Atom.attrlist.radical)
this.cip = getValueOrDefault(attributes.cip, Atom.attrlist.cip)
this.charge = getValueOrDefault(attributes.charge, Atom.attrlist.charge)
this.rglabel = getValueOrDefault(attributes.rglabel, Atom.attrlist.rglabel)
this.attpnt = getValueOrDefault(attributes.attpnt, Atom.attrlist.attpnt)
Expand Down
3 changes: 2 additions & 1 deletion packages/ketcher-core/src/domain/entities/functionalGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import { ReSGroup } from 'application/render'
import assert from 'assert'
import { FunctionalGroupsProvider } from '../helpers'
import { Atom } from './atom'
import { Bond } from './bond'
import { Pool } from './pool'
import { SGroup } from './sgroup'
Expand Down Expand Up @@ -235,7 +236,7 @@ export class FunctionalGroup {
}

static isAtomInContractedFunctionalGroup(
atom,
atom: Atom,
sgroups,
functionalGroups,
sgroupsFromReStruct: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function atomToStruct(source) {
ifDef(params, 'explicitValence', source.explicitValence)
ifDef(params, 'isotope', source.isotope)
ifDef(params, 'radical', source.radical)
ifDef(params, 'cip', source.cip)
ifDef(params, 'attpnt', source.attachmentPoints)
// stereo
ifDef(params, 'stereoLabel', source.stereoLabel)
Expand Down
2 changes: 1 addition & 1 deletion packages/ketcher-react/src/script/editor/tool/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class SelectTool {
)
const atomFromStruct = atomId !== null && struct.atoms.get(atomId)?.a
if (
atomId !== null &&
atomFromStruct &&
!FunctionalGroup.isAtomInContractedFunctionalGroup(
// TODO: examine if this code is really needed, seems like its a hack
atomFromStruct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export const atom = {
],
default: 0
},
cip: {
title: 'CIP',
type: 'string',
enum: ['R', 'S', 'r', 's']
},
ringBondCount: {
title: 'Ring bond count',
enum: [0, -2, -1, 2, 3, 4],
Expand Down

0 comments on commit c608227

Please sign in to comment.