<commit_before>/* <NAME> Copyright @2015
 * Manage the drawing context/canvas as a React View
 */

"use strict";


let _                   = require('lodash');
import React            from 'react';
import ViewerControls   from './viewer_controls';
import CompassView      from '../compass/compass';
import LoadQueueView    from '../load_queue';

// Import shaders
require('./shaders/VelvetyShader');


/*************************************************************************/

class ViewButton extends React.Component {
  constructor(props) {
    super(props);
  }
  
  render() {
    let icon = "unlock";
    if (this.props.locked)
      icon = "lock";
    
    return <div className="resetview">
      <span
        className={"glyphicons glyphicons-eye-open" + (this.props.locked ? ' locked' : '')}
        onClick={this.props.alignCb}
      />
      <span
        className={"lock glyphicons glyphicons-" + icon + (this.props.locked ? ' locked' : '')}
        onClick = {this.props.toggleLock}
      />
    </div>;
  }
}

/*************************************************************************/

export default class CADView extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            modelTree: {},
            isViewChanging: false,
            lastHovered: undefined,
            lockedView: true
        };
        this.handleResize   = this.handleResize.bind(this);
        this.onShellLoad    = this.onShellLoad.bind(this);
        this.onModelAdd     = this.onModelAdd.bind(this);
        this.onModelRemove  = this.onModelRemove.bind(this);
        this.invalidate     = this.invalidate.bind(this);
        this.onKeypress     = this.onKeypress.bind(this);
        this.onMouseUp      = this.onMouseUp.bind(this);
        this.onMouseMove    = this.onMouseMove.bind(this);
        this.onTreeClick    = this.onTreeClick.bind(this);
        this.onTreeChange   = this.onTreeChange.bind(this);
        this.onTreeNodeEnterExit = this.onTreeNodeEnterExit.bind(this);
        this.alignToolView = this.alignToolView.bind(this);
    }

    onShellLoad(event) {
        // Get around the fact that viewerControls calls change a bunch at startup
        this.setState({'isViewChanging':true});
        this.invalidate(event);
    }
    
    onModelAdd(event) {
        //This is where the NC model is being loaded into the CADview
        let model = this.props.manager._models[event.path];
        // Add the model to the scene
        this.annotationScene.add(   model.getAnnotation3D());
        this.geometryScene.add(     model.getObject3D());
        this.overlayScene.add(      model.getOverlay3D());
        // calculate the scene's radius for draw distance calculations
        this.updateSceneBoundingBox(model.getBoundingBox());
        
        let sel = this.props.manager.getSelected();
        
        if (_.find(_.values(sel[0]._objects), {usage: 'machine'})) {
            this.setState({lockedView: false});
            // set the default view
            this.zoomToFit(sel);
            this.invalidate();
        }

        
        // Update the model tree
        let tree = this.props.manager.getTree();
        this.setState({ modelTree:tree });
    }

    onModelRemove(event) {
        console.log('ModelRemove: ' + event.path);
        // TODO: Need to do anything here?
        // Update the model tree
        let tree = this.props.manager.getTree();
        this.setState({ modelTree: tree });
    }

    onKeypress(event) {
        switch(event.keyCode || event.charCode || event.which) {

            case 27:
                this.props.openProperties(null);
                break;

            // Go to special viewing postion on 'a'
            case 97:
                //console.log(this.camera);
                this.alignToolView(this.props.manager.getSelected());
                this.invalidate();
                break;
            // Explode on 'x' key pressed
            case 120:
                this.props.manager.explode(10);
                break;
            // Unexplode on 's' key pressed
            case 115:
                this.props.manager.explode(-10);
                break;
            // 'q' reset all elements
            case 113:
                this.props.manager.clear();
                break;
            // 'o' to toggle transparency
            case 111:
                this.props.manager.toggleOpacity();
                break;
            // 'z' to zoomToFit
            case 122:
                let objs = this.props.manager.getSelected();
                this.zoomToFit(objs);
                break;
            // 'j' hide/show element
            case 106:
                this.props.manager.toggleVisibility();
                break;
        }
        this.invalidate({ tree: true });
    }
    
    componentWillUpdate(nextProps, nextState) {
      if (this.props.selectedEntity !== null && this.props.selectedEntity.type === 'tolerance') {
        if (nextProps.selectedEntity === null ||
          nextProps.selectedEntity !== this.props.selectedEntity) {

          let objs = this.props.manager.getSelected();
          let shell = _.find(_.values(objs[0]._objects), {usage: 'tobe'});
          if (shell) {
            let faces = shell.model._geometry.getAttribute('faces');
            let colors = shell.model._geometry.getAttribute('color');
            
            let indices = _.map(this.props.selectedEntity.faces, (id) => faces.array[id]);

            _.each(indices, (index) => {
              for (let i=index.start; i < index.end; i++) {
                let r = colors.array[i * colors.itemSize] *  2.0 - 1;
                let g = colors.array[i * colors.itemSize + 1] * 2.0 - 1;
                let b = colors.array[i * colors.itemSize + 2] * 2.0;
                colors.setXYZ(i, r, g, b);
              }
            });
            colors.needsUpdate = true;
            this.invalidate();
          }
        }
      }
    
      if ((this.props.selectedEntity === null ||
          this.props.selectedEntity !== nextProps.selectedEntity) &&
          nextProps.selectedEntity !== null &&
          nextProps.selectedEntity.type === 'tolerance') {

        if (nextProps.ws === 90424) {
          let objs = nextProps.manager.getSelected();
          let shell = _.find(_.values(objs[0]._objects), {usage: 'tobe'});
          if (shell) {
            let faces = shell.model._geometry.getAttribute('faces');
            let colors = shell.model._geometry.getAttribute('color');
            
            let indices = _.map(nextProps.selectedEntity.faces, (id) => faces.array[id]);

            _.each(indices, (index) => {
              for (let i=index.start; i < index.end; i++) {
                let r = (colors.array[i * colors.itemSize] + 1) / 2.0;
                let g = (colors.array[i * colors.itemSize + 1] + 1) / 2.0;
                let b = (colors.array[i * colors.itemSize + 2] + 0) / 2.0;
                colors.setXYZ(i, r, g, b);
              }
            });
            colors.needsUpdate = true;
            this.invalidate();
          }
        }
      }
    }

    componentWillMount() {
        this.sceneCenter = new THREE.Vector3(0,0,0);
        this.sceneRadius = 10000;
        this.props.manager.addEventListener("model:add", this.onModelAdd);
        this.props.manager.addEventListener("model:remove", this.onModelRemove);
        this.props.manager.addEventListener("shellLoad", this.onShellLoad);
        this.props.manager.addEventListener("annotationLoad", this.invalidate);
        this.props.manager.addEventListener("invalidate", this.invalidate);
        // Keybased events
        window.addEventListener("keydown", this.onKeypress, true);
        window.addEventListener("keypress", this.onKeypress, true);
    }

    componentDidMount() {
        let self = this;
        // RENDERER
        this.canvasParent = document.getElementById('cadjs-container');
        this.canvas = document.getElementById('cadjs-canvas');
        this.renderer = new THREE.WebGLRenderer({<commit_msg>Added some comments and set up for actual workingsteps<commit_after>/* <NAME> Copyright @2015
 * Manage the drawing context/canvas as a React View
 */

"use strict";


let _                   = require('lodash');
import React            from 'react';
import ViewerControls   from './viewer_controls';
import CompassView      from '../compass/compass';
import LoadQueueView    from '../load_queue';

// Import shaders
require('./shaders/VelvetyShader');


/*************************************************************************/

class ViewButton extends React.Component {
  constructor(props) {
    super(props);
  }
  
  render() {
    let icon = "unlock";
    if (this.props.locked)
      icon = "lock";
    
    return <div className="resetview">
      <span
        className={"glyphicons glyphicons-eye-open" + (this.props.locked ? ' locked' : '')}
        onClick={this.props.alignCb}
      />
      <span
        className={"lock glyphicons glyphicons-" + icon + (this.props.locked ? ' locked' : '')}
        onClick = {this.props.toggleLock}
      />
    </div>;
  }
}

/*************************************************************************/

export default class CADView extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            modelTree: {},
            isViewChanging: false,
            lastHovered: undefined,
            lockedView: true
        };
        this.handleResize   = this.handleResize.bind(this);
        this.onShellLoad    = this.onShellLoad.bind(this);
        this.onModelAdd     = this.onModelAdd.bind(this);
        this.onModelRemove  = this.onModelRemove.bind(this);
        this.invalidate     = this.invalidate.bind(this);
        this.onKeypress     = this.onKeypress.bind(this);
        this.onMouseUp      = this.onMouseUp.bind(this);
        this.onMouseMove    = this.onMouseMove.bind(this);
        this.onTreeClick    = this.onTreeClick.bind(this);
        this.onTreeChange   = this.onTreeChange.bind(this);
        this.onTreeNodeEnterExit = this.onTreeNodeEnterExit.bind(this);
        this.alignToolView = this.alignToolView.bind(this);
    }

    onShellLoad(event) {
        // Get around the fact that viewerControls calls change a bunch at startup
        this.setState({'isViewChanging':true});
        this.invalidate(event);
    }
    
    onModelAdd(event) {
        //This is where the NC model is being loaded into the CADview
        let model = this.props.manager._models[event.path];
        // Add the model to the scene
        this.annotationScene.add(   model.getAnnotation3D());
        this.geometryScene.add(     model.getObject3D());
        this.overlayScene.add(      model.getOverlay3D());
        // calculate the scene's radius for draw distance calculations
        this.updateSceneBoundingBox(model.getBoundingBox());
        
        let sel = this.props.manager.getSelected();
        
        if (_.find(_.values(sel[0]._objects), {usage: 'machine'})) {
            this.setState({lockedView: false});
            // set the default view
            this.zoomToFit(sel);
            this.invalidate();
        }

        
        // Update the model tree
        let tree = this.props.manager.getTree();
        this.setState({ modelTree:tree });
    }

    onModelRemove(event) {
        console.log('ModelRemove: ' + event.path);
        // TODO: Need to do anything here?
        // Update the model tree
        let tree = this.props.manager.getTree();
        this.setState({ modelTree: tree });
    }

    onKeypress(event) {
        switch(event.keyCode || event.charCode || event.which) {

            case 27:
                this.props.openProperties(null);
                break;

            // Go to special viewing postion on 'a'
            case 97:
                //console.log(this.camera);
                this.alignToolView(this.props.manager.getSelected());
                this.invalidate();
                break;
            // Explode on 'x' key pressed
            case 120:
                this.props.manager.explode(10);
                break;
            // Unexplode on 's' key pressed
            case 115:
                this.props.manager.explode(-10);
                break;
            // 'q' reset all elements
            case 113:
                this.props.manager.clear();
                break;
            // 'o' to toggle transparency
            case 111:
                this.props.manager.toggleOpacity();
                break;
            // 'z' to zoomToFit
            case 122:
                let objs = this.props.manager.getSelected();
                this.zoomToFit(objs);
                break;
            // 'j' hide/show element
            case 106:
                this.props.manager.toggleVisibility();
                break;
        }
        this.invalidate({ tree: true });
    }
    
    componentWillUpdate(nextProps, nextState) {
      
      // Unhighlight anything that's not being shown
      if (this.props.selectedEntity !== null && this.props.selectedEntity.type === 'tolerance') {
        if (nextProps.selectedEntity === null ||
          nextProps.selectedEntity !== this.props.selectedEntity) {

          let objs = this.props.manager.getSelected();
          let shell = _.find(_.values(objs[0]._objects), {usage: 'tobe'});
          if (shell) {
            let faces = shell.model._geometry.getAttribute('faces');
            let colors = shell.model._geometry.getAttribute('color');
            
            let indices = _.map(this.props.selectedEntity.faces, (id) => faces.array[id]);

            // unhighlight each old tolerance face
            _.each(indices, (index) => {
              for (let i=index.start; i < index.end; i++) {
                let r = colors.array[i * colors.itemSize] *  2.0 - 1;
                let g = colors.array[i * colors.itemSize + 1] * 2.0 - 1;
                let b = colors.array[i * colors.itemSize + 2] * 2.0 - 0.6;
                colors.setXYZ(i, r, g, b);
              }
            });
            colors.needsUpdate = true;
            this.invalidate();
          }
        }
      }

      // now highlight any tolerances present in current workingstep
      if ((this.props.selectedEntity === null ||
          this.props.selectedEntity !== nextProps.selectedEntity) &&
          nextProps.selectedEntity !== null &&
          nextProps.selectedEntity.type === 'tolerance') {

        let workpiece = nextProps.toleranceCache[nextProps.selectedEntity.workpiece];

        // check if the selected tolerance/wp is used in the current WS
        if (workpiece.workingsteps.indexOf(nextProps.ws) > 0) {
          let objs = nextProps.manager.getSelected();
          let shell = _.find(_.values(objs[0]._objects), {usage: 'tobe'});
          if (shell) {
            let faces = shell.model._geometry.getAttribute('faces');
            let colors = shell.model._geometry.getAttribute('color');
            
            let indices = _.map(nextProps.selectedEntity.faces, (id) => faces.array[id]);

            // highlight in bright yellow for each face in the tolerance
            _.each(indices, (index) => {
              for (let i=index.start; i < index.end; i++) {
                let r = (colors.array[i * colors.itemSize] + 1) / 2.0;
                let g = (colors.array[i * colors.itemSize + 1] + 1) / 2.0;
                let b = (colors.array[i * colors.itemSize + 2] + 0.6) / 2.0;
                colors.setXYZ(i, r, g, b);
              }
            });
            colors.needsUpdate = true;
            this.invalidate();
          }
        }
      }
    }

    componentWillMount() {
        this.sceneCenter = new THREE.Vector3(0,0,0);
        this.sceneRadius = 10000;
        this.props.manager.addEventListener("model:add", this.onModelAdd);
        this.props.manager.addEventListener("model:remove", this.onModelRemove);
        this.props.manager.addEventListener("shellLoad", this.onShellLoad);
        this.props.manager.addEventListener("annotationLoad", this.invalidate);
        this.props.manager.addEventListener("invalidate", this.invalidate);
        // Keybased events
        window.addEventListener("keydown", this.onKeypress, true);
        window.addEventListener("keypress", this.onKeypress, true);
    }

    componentDidMount() {
        let self = this;
        // RENDERER
        this.canvasParent = document.getElementById('cadjs-container');
        this.canvas = document.getElementById('cadjs-canvas');
        this.renderer = new THREE.WebGLRenderer({
{'fraction_non_alphanumeric': 0.1401044480461012, 'fraction_numerical': 0.008463893390959841, 'mean_word_length': 1.8978952861367195, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2738155', 'n_tokens_mistral': 4727, 'n_tokens_neox': 4501, 'n_words': 949}