protected override Rhino.Commands.Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode) { pluginObj = ((nishanchiPlugin)this.PlugIn); RhinoView viewObj = doc.Views.ActiveView; RhinoViewport viewPortObj = viewObj.ActiveViewport; Vector3d originalCameraVector = viewPortObj.CameraDirection; Point3d originalCameraLocation = viewPortObj.CameraLocation; Mesh meshObj; Boolean logging = false; StreamWriter logWriter = null; String reportString; String logString; String printCommandStr; if (((nishanchiPlugin)this.PlugIn).logEnabled()) { logWriter = new StreamWriter(((nishanchiPlugin)this.PlugIn).getNewLogFile()); logWriter.WriteLine("Printing..."); logging = true; } if (!pluginObj.trackerConnected) { RhinoApp.WriteLine("Tracker disconnected"); return Rhino.Commands.Result.Failure; } connectKbEvt(); Rhino.Input.Custom.GetObject go = new Rhino.Input.Custom.GetObject(); go.SetCommandPrompt("Select the mesh to print"); go.Get(); Result rc = go.CommandResult(); if (rc != Rhino.Commands.Result.Success) { disconnectKbEvt(); return rc; } meshObj = new Mesh(); if (go.ObjectCount == 1){ ObjRef tmpRef = new ObjRef(go.Object(0).ObjectId); meshObj = tmpRef.Mesh(); if (meshObj == null){ disconnectKbEvt(); return rc; } } createNozzles(doc); cameraPoint = new tPoint(Nozzle.XOffset + 100.0, Nozzle.YOffset, Nozzle.ZOffset + (Nozzle.ZMultiplier * ((numNozzles-1)/2))); fastrak trackerObj = pluginObj.trackerObj; int numPoints = 0; Boolean retval; RhinoApp.WriteLine(string.Format("Starting continuous mode on the tracker.")); pluginObj.trackerObj.setContinuous(); RhinoApp.WriteLine(string.Format("Printing mode enabled now.")); running = true; while (running) { retval = trackerObj.readFrame(); if (retval) { //q0,q1,q2,q3 Quaternion begin //Compute the rotation matrix reportString = String.Format("{0},{1},{2}", trackerObj.x, trackerObj.y, trackerObj.z); logString = String.Format("p,{0},{1},{2},{3},{4},{5},{6},{7}", numPoints, trackerObj.x, trackerObj.y, trackerObj.z, trackerObj.q0, trackerObj.q1, trackerObj.q2, trackerObj.q3); RhinoApp.WriteLine(reportString); if (logging) { logWriter.WriteLine(logString); } rxTxTx = new Point3d(trackerObj.x, trackerObj.y, trackerObj.z); orientationQuat = new Quaternion(trackerObj.q0, trackerObj.q1, trackerObj.q2, trackerObj.q3); orientationQuat.GetRotation(out angle, out axis); rotMat = Transform.Rotation(angle, axis, origin); // Compute the new positions for the nozzlePoints and also compute the new lines for (int i = 0; i < numNozzles; i++) { nozzles[i].computeTxTx(rotMat, rxTxTx); } //And, I move the view around if ((numPoints % 100) == 0) { cameraPoint.computeTxTx(rotMat, rxTxTx); viewPortObj.SetCameraLocation(cameraPoint.txTx(), true); viewPortObj.SetCameraDirection(nozzles[4].vector(), true); viewPortObj.Rotate(Math.PI / 2, nozzles[4].vector(), cameraPoint.txTx()); cameraUpTx = rotMat * cameraUpRx; viewPortObj.CameraUp = cameraUpTx; removeNozzleLinesFromDocument(doc); addNozzleLinesToDocument(doc); doc.Views.ActiveView.Redraw(); RhinoApp.Wait(); } //Compute ray intersections if ((numPoints % 4) == 0) { //RhinoApp.WriteLine("Calculating intersections.."); for (int i = 0; i < numNozzles; i++) { nozzles[i].rayIntersectionDistance = Intersection.MeshRay(meshObj,nozzles[i].ray()); } printCommandStr = printCommand(); //RhinoApp.WriteLine(printCommandStr); if (logging) { logString = String.Format("c,{0},{1}", numPoints, printCommandStr); logWriter.WriteLine(logString); } } numPoints++; } } running = false; //clean up removeNozzleLinesFromDocument(doc); doc.Views.ActiveView.Redraw(); RhinoApp.WriteLine("Removing printHead objects!!"); if (logging) { logWriter.Close(); ((nishanchiPlugin)this.PlugIn).closeLogFile(); } //stop the tracker RhinoApp.WriteLine(string.Format("Stopping continuous mode on the tracker.")); pluginObj.trackerObj.stopContinuous(); disconnectKbEvt(); RhinoApp.WriteLine(string.Format("I guess you don't wanna print anymore, well who cares! Not me!")); //setup the viewport as it was viewPortObj.SetCameraLocation(originalCameraLocation, true); viewPortObj.SetCameraDirection(originalCameraVector, true); return Rhino.Commands.Result.Success; }
//This guy doesn't actually do something //You need it to debug though public void read100Frames(nishanchiPlugin pluginObj) { StreamWriter logWriter = null; String logString; bool logging = false; Stopwatch stopWatch = new Stopwatch(); long millisec; if (pluginObj.logEnabled()) { logWriter = new StreamWriter(pluginObj.getNewLogFile()); logWriter.WriteLine("Fastrak usb profiling..."); logging = true; } List<string> parts = new List<string>(); for (int i = 0; i <= 1000; i++) { stopWatch.Reset(); stopWatch.Start(); string line = serialPort.ReadLine(); stopWatch.Stop(); millisec = stopWatch.ElapsedMilliseconds; if (logging) { logString = String.Format("{0}, {1}, {2}",i,millisec,line); logWriter.WriteLine(logString); } try { x = Single.Parse(line.Substring(4, 6)); y = Single.Parse(line.Substring(11, 6)); z = Single.Parse(line.Substring(18, 6)); q0 = Single.Parse(line.Substring(24, 7)); q1 = Single.Parse(line.Substring(31, 7)); q2 = Single.Parse(line.Substring(38, 7)); q3 = Single.Parse(line.Substring(45, 7)); } catch (Exception e) { rhinoPrint("Exception in parsing.." + line); continue; } RhinoApp.WriteLine(String.Format("Here {0},{1},{2},{3},{4},{5},{6}",x,y,z,q0,q1,q2,q3)); RhinoApp.WriteLine(line); } logWriter.Close(); pluginObj.closeLogFile(); }