Skip to content

Commit

Permalink
imodeljs-meets-csv
Browse files Browse the repository at this point in the history
  • Loading branch information
roopksaini committed Sep 16, 2019
1 parent fa2d178 commit 8401151
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 0 deletions.
22 changes: 22 additions & 0 deletions assets/testdata.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
YES,AT_HRZF7AQ5_1FC
YES,AT_HRZF7AQ5_1FJ
NO,AT_HRZF7AQ5_4IO
NO,AT_HRZF7AQ5_3RO
YES,AT_HRZF7AQ5_3RQ
NO,AT_HRZF7AQ5_40X
NO,AT_HRZF7AQ5_4GO
OPEN ENDED,AT_HRZF7AQ5_4GU
NO,AT_HRZF7AQ5_4HX
YES,AT_HRZF7AQ5_4I2
NO,AT_HRZF7AQ5_4I8
YES,AT_HRZF7AQ5_5F9
OPEN ENDED,AT_HRZF7AQ5_4II
NO,AT_HRZF7AQ5_51E
OPEN ENDED,AT_HRZF7AQ5_549
OPEN ENDED,AT_HRZF7AQ5_4HU
YES,AT_HRZF7AQ5_4I5
NO,AT_HRZF7AQ5_4IM
YES,AT_HRZF7AQ5_51B
OPEN ENDED,AT_HRZF7AQ5_51E
OPEN ENDED,AT_HRZF7AQ5_549
OPEN ENDED,AT_HRZF7AQ5_5F8
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"@bentley/presentation-testing": "1.4.0",
"@bentley/ui-components": "1.4.0",
"@bentley/ui-core": "1.4.0",
"csv-parse": "^4.4.1",
"body-parser": "^1.18",
"bunyan": "^1.8.12",
"chai-jest-snapshot": "^2.0.0",
Expand All @@ -117,6 +118,7 @@
"@types/puppeteer": "^1.10.0",
"@types/react": "^16.4.14",
"@types/react-dom": "16.0.7",
"@types/csv-parse": "^1.1.12",
"chai": "^4.2.0",
"chromedriver": "^2.34.1",
"electron": "^4.0.1",
Expand Down
24 changes: 24 additions & 0 deletions public/map_pin_green.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions public/map_pin_red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions public/map_pin_yellow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions src/backend/FileReaderRpcImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { RpcManager, IModelToken } from "@bentley/imodeljs-common";
import { FileReaderRpcInterface } from "../common/FileReaderRpcInterface";
import { IModelDb } from "@bentley/imodeljs-backend";
import * as fs from "fs";
import * as parse from "csv-parse/lib/sync";

export class FileReaderRpcImpl extends FileReaderRpcInterface {
public static register() { RpcManager.registerImpl(FileReaderRpcInterface, FileReaderRpcImpl); }
private _filePath = "assets/testdata.csv";

public async fetchInfo(_token: IModelToken): Promise<any[]> {
const data: string = fs.readFileSync(this._filePath, "utf8");
let info = parse(data, {delimiter: ",", columns: ["status", "component_id"]});
info = await this.fetchPositions(info, _token);
return info;
}

private async fetchPositions(info: any[], token: IModelToken) {
let componentList = "(";
const count = info.length;
// prepare list of component ids
info.forEach((value: any, index: number) => {
componentList += "'" + value.component_id + "'";
componentList += (++index !== count) ? ", " : ")";
});

const query = `SELECT piping.Component_id, physical.Origin
FROM AutoPlantPDWPersistenceStrategySchema.PipingComponent piping
JOIN Bis.ElementOwnsChildElements link ON piping.ECInstanceId = link.SourceECInstanceId
JOIN Bis.PhysicalElement physical ON link.TargetECInstanceId = physical.ECInstanceId
WHERE piping.Component_id IN ${componentList}`;

const imodel = IModelDb.find(token);
const rows = [];
for await (const row of imodel.query(query)) rows.push(row);

rows.forEach((row) => {
const index = info.findIndex((x) => x.component_id === row.cOMPONENT_ID);
if (index > 0) info[index].position = row.origin;
});

return info;
}
}
3 changes: 3 additions & 0 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Presentation } from "@bentley/presentation-backend";
import getSupportedRpcs from "../common/rpcs";
import { RpcInterfaceDefinition } from "@bentley/imodeljs-common";
import setupEnv from "../common/configuration";
import { FileReaderRpcImpl } from "./FileReaderRpcImpl";

// setup environment
setupEnv();

Expand All @@ -18,6 +20,7 @@ Logger.initializeToConsole();

// initialize imodeljs-backend
IModelHost.startup();
FileReaderRpcImpl.register();

// initialize presentation-backend
Presentation.initialize({
Expand Down
14 changes: 14 additions & 0 deletions src/common/FileReaderRpcInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*---------------------------------------------------------------------------------------------
| $Copyright: (c) 2017 Bentley Systems, Incorporated. All rights reserved. $
*--------------------------------------------------------------------------------------------*/
import { RpcInterface, RpcManager, IModelToken } from "@bentley/imodeljs-common";

export abstract class FileReaderRpcInterface extends RpcInterface {

public static interfaceVersion = "1.0.0";
public static interfaceName = "FileReaderRpcInterface";
public static types = () => [IModelToken];

public static getClient(): FileReaderRpcInterface { return RpcManager.getClientForInterface(this); }
public async fetchInfo (_token: IModelToken): Promise<any[]> { return this.forward.apply(this, arguments as any) as any; }
}
2 changes: 2 additions & 0 deletions src/common/rpcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { RpcInterfaceDefinition, IModelReadRpcInterface, IModelTileRpcInterface, SnapshotIModelRpcInterface } from "@bentley/imodeljs-common";
import { PresentationRpcInterface } from "@bentley/presentation-common";
import { FileReaderRpcInterface } from "./FileReaderRpcInterface";

/**
* Returns a list of RPCs supported by this application
Expand All @@ -12,6 +13,7 @@ export default function getSupportedRpcs(): RpcInterfaceDefinition[] {
return [
IModelReadRpcInterface,
IModelTileRpcInterface,
FileReaderRpcInterface,
PresentationRpcInterface,
SnapshotIModelRpcInterface,
];
Expand Down
8 changes: 8 additions & 0 deletions src/frontend/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import TreeWidget from "./Tree";
import ViewportContentControl from "./Viewport";
import "@bentley/icons-generic-webfont/dist/bentley-icons-generic-webfont.css";
import "./App.css";
import { FileReaderRpcInterface } from "../../common/FileReaderRpcInterface";
import { GasketDecorator } from "./GasketDecorator";

// tslint:disable: no-console
// cSpell:ignore imodels
Expand Down Expand Up @@ -136,6 +138,12 @@ export default class App extends React.Component<{}, AppState> {
return;
}
try {

const info: any[] = await FileReaderRpcInterface.getClient().fetchInfo(imodel.iModelToken);

const decorator = new GasketDecorator(info);
IModelApp.viewManager.addDecorator(decorator);

// attempt to get a view definition
const viewDefinitionId = imodel ? await this.getFirstViewDefinitionId(imodel) : undefined;
this.setState({ imodel, viewDefinitionId });
Expand Down
27 changes: 27 additions & 0 deletions src/frontend/components/GasketDecorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DecorateContext, Marker, Decorator } from "@bentley/imodeljs-frontend";
import { GasketMarker } from "./GasketMarker";

export class GasketDecorator implements Decorator {
protected _markers: Marker[] = [];

constructor(info: any[]) {
info.forEach( (component) => {
if (component.position && component.status) this.addMarker(component);
});
}

private addMarker(component: any) {
const marker = new GasketMarker(
{ x: component.position.x, y: component.position.y, z: component.position.z },
{ x: 50, y: 50 },
component.status,
);
this._markers.push(marker);
}

public decorate(context: DecorateContext): void {
this._markers.forEach((marker) => {
marker.addDecoration(context);
});
}
}
16 changes: 16 additions & 0 deletions src/frontend/components/GasketMarker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Marker, imageElementFromUrl } from "@bentley/imodeljs-frontend";
import { XYAndZ, XAndY } from "@bentley/geometry-core";

const statusToUrl = new Map<string, string>([
["YES", "map_pin_green.svg"],
["NO", "map_pin_red.svg"],
["OPEN ENDED", "map_pin_yellow.svg"],
]);

export class GasketMarker extends Marker {
constructor(worldLocation: XYAndZ, size: XAndY, status: string) {
super(worldLocation, size);
const imageUrl = statusToUrl.get(status);
if (imageUrl) this.setImage(imageElementFromUrl(imageUrl));
}
}

0 comments on commit 8401151

Please sign in to comment.