Files
ProcessCodeConverter/src/processors/CncConverter/CncConverter.ts

149 lines
3.9 KiB
TypeScript

export class CncConverter implements ICncWriter {
private lines: string[] = []
private nodes: any[] = [];
private actionRecord: CncAction[] = []
get cncActions(): CncAction[] {
return this.actionRecord
}
config: CncConverterConfig = {
isNcFileComment: true,
/** 换行符 个别文件需要用 ;\n 结束*/
lineBreak: '\n',
leaderChar: '//',
}
async doXML(data: CncTemplateParams[]) {
// let line = []
for (const template of data) {
await this.doCncTemplate(template)
}
}
async doCncTemplate(templateItem: CncTemplateParams, level: number = 0) {
/** 行缩进 内容 */
let tabContent = ` `
/** 实际行缩进输出内容 */
let tabVal = '';
for (let i = 0; i < level; i++) {
// 按照节点层级 生成缩进内容
tabVal = tabVal + tabContent;
}
let node: any[] = []
node.push(tabVal)
let startCode = `<${templateItem.templateName}`
node.push(startCode)
for (const propertyInfo of templateItem.propertyList) {
let propertyItem = `${propertyInfo.propertyName}="${propertyInfo.propertyValue}"`
node.push(propertyItem)
}
let endStr = ' >'
if (templateItem.templateEndType == TemplateEndTargetType.SingleEnd) {
endStr = ' />'
}
node.push(endStr)
this.lines.push(node.join(' '))
if (Array.isArray(templateItem.children) && templateItem.children.length > 0) {
// 子节点 内容
for (const kid of templateItem.children) {
await this.doCncTemplate(kid, level + 1)
}
}
/** 结尾 */
let endCode = templateItem.templateEndType == TemplateEndTargetType.DoubleEnd ? `</ ${templateItem.templateName}>` : ''
if(endCode){
this.lines.push((tabVal + endCode))
}
}
recordAction(type: CncActionType): string {
const id = this.createActionId();
const act: CncAction = {
id: id,
type,
lineIndex: this.lines.length
};
this.comment(`CMP ${act.id} ${act.type}`);
this.actionRecord.push(act);
return id;
}
private _actionIdx = 0;
private createActionId() {
return `A${this._actionIdx++}`;
}
comment(content: string): string {
let markContent = content + this.config.lineBreak
let isShowMark = this.config.isNcFileComment || false
if (isShowMark) {
let leaderChar = this.config.leaderChar || ''
markContent = `${leaderChar} ${markContent}`
} else {
markContent = ''
}
return markContent + this.config.lineBreak
}
}
export interface ICncWriter {
get cncActions(): CncAction[]
}
export interface CncAction {
readonly id: string;
readonly type: CncActionType;
readonly lineIndex: number;
// parent: CncAction;
// children: CncAction[]
}
export type CncActionType = string
export type CncTemplateParams = {
/** 标签名 */
templateName: string
/** 属性列表 */
propertyList: CncTemplatePropertyType[]
/** 子标签 */
children?: CncTemplateParams[]
/** 标签结束方式 */
templateEndType?: TemplateEndTargetType
}
export interface CncTemplatePropertyType {
/** 属性名 */
propertyName: string
/** 属性值 */
propertyValue?: string | number
}
/**
* 节点结束类型
* 标记文本语言的结尾形式
*
* <div></div>
* <div />
*/
export enum TemplateEndTargetType {
/** 这种 <div></div>*/
DoubleEnd = 0,
/** 这种 <div />*/
SingleEnd = 1
}
export type CncConverterConfig = {
/** 换行符 个别文件需要用 ;\n 结束*/
lineBreak?: string
/** 是否添加注释信息 */
isNcFileComment?: boolean
/** 注释标识符 */
leaderChar?: string
}