usingSystem;partialclassExamples{publicstaticRhino.Commands.ResultAddBackgroundBitmap(Rhino.RhinoDoc doc){// Allow the user to select a bitmap filevar fd =newRhino.UI.OpenFileDialog{Filter="Image Files (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg"};if(!fd.ShowOpenDialog())returnRhino.Commands.Result.Cancel;// Verify the file that was selectedSystem.Drawing.Image image;try{
image =System.Drawing.Image.FromFile(fd.FileName);}catch(Exception){returnRhino.Commands.Result.Failure;}// Allow the user to pick the bitmap originvar gp =newRhino.Input.Custom.GetPoint();
gp.SetCommandPrompt("Bitmap Origin");
gp.ConstrainToConstructionPlane(true);
gp.Get();if(gp.CommandResult()!=Rhino.Commands.Result.Success)return gp.CommandResult();// Get the view that the point was picked in.// This will be the view that the bitmap appears in.var view = gp.View();if(view ==null){
view = doc.Views.ActiveView;if(view ==null)returnRhino.Commands.Result.Failure;}// Allow the user to specify the bitmap width in model unitsvar gn =newRhino.Input.Custom.GetNumber();
gn.SetCommandPrompt("Bitmap width");
gn.SetLowerLimit(1.0,false);
gn.Get();if(gn.CommandResult()!=Rhino.Commands.Result.Success)return gn.CommandResult();// Cook up some scale factorsvar w = gn.Number();var image_width = image.Width;var image_height = image.Height;var h = w *(image_height / image_width);var plane = view.ActiveViewport.ConstructionPlane();
plane.Origin= gp.Point();
view.ActiveViewport.SetTraceImage(fd.FileName, plane, w, h,false,false);
view.Redraw();returnRhino.Commands.Result.Success;}}
importRhinoimport scriptcontext
importSystem.Windows.Forms.DialogResultimportSystem.Drawing.ImagedefAddBackgroundBitmap():# Allow the user to select a bitmap file
fd =Rhino.UI.OpenFileDialog()
fd.Filter="Image Files (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg"if fd.ShowDialog()!=System.Windows.Forms.DialogResult.OK:returnRhino.Commands.Result.Cancel# Verify the file that was selected
image =Nonetry:
image =System.Drawing.Image.FromFile(fd.FileName)except:returnRhino.Commands.Result.Failure# Allow the user to pick the bitmap origin
gp =Rhino.Input.Custom.GetPoint()
gp.SetCommandPrompt("Bitmap Origin")
gp.ConstrainToConstructionPlane(True)
gp.Get()if gp.CommandResult()!=Rhino.Commands.Result.Success:return gp.CommandResult()# Get the view that the point was picked in.# This will be the view that the bitmap appears in.
view = gp.View()if view isNone:
view = scriptcontext.doc.Views.ActiveViewif view isNone:returnRhino.Commands.Result.Failure# Allow the user to specify the bitmap with in model units
gn =Rhino.Input.Custom.GetNumber()
gn.SetCommandPrompt("Bitmap width")
gn.SetLowerLimit(1.0,False)
gn.Get()if gn.CommandResult()!=Rhino.Commands.Result.Success:return gn.CommandResult()# Cook up some scale factors
w = gn.Number()
h = w *(image.Width/ image.Height)
plane = view.ActiveViewport.ConstructionPlane()
plane.Origin= gp.Point()
view.ActiveViewport.SetTraceImage(fd.FileName, plane, w, h,False,False)
view.Redraw()returnRhino.Commands.Result.Successif __name__=="__main__":AddBackgroundBitmap()
PartialClassExamplesPublicSharedFunctionAddBackgroundBitmap(ByVal doc AsRhino.RhinoDoc)AsRhino.Commands.Result' Allow the user to select a bitmap fileDim fd AsNewRhino.UI.OpenFileDialog()
fd.Filter="Image Files (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg"If fd.ShowDialog()<>System.Windows.Forms.DialogResult.OKThenReturnRhino.Commands.Result.CancelEndIf' Verify the file that was selectedDim image AsSystem.Drawing.ImageTry
image =System.Drawing.Image.FromFile(fd.FileName)Catch generatedExceptionName AsExceptionReturnRhino.Commands.Result.FailureEndTry' Allow the user to pick the bitmap originDim gp AsNewRhino.Input.Custom.GetPoint()
gp.SetCommandPrompt("Bitmap Origin")
gp.ConstrainToConstructionPlane(True)
gp.Get()If gp.CommandResult()<>Rhino.Commands.Result.SuccessThenReturn gp.CommandResult()EndIf' Get the view that the point was picked in.' This will be the view that the bitmap appears in.Dim view AsRhino.Display.RhinoView= gp.View()If view IsNothingThen
view = doc.Views.ActiveViewIf view IsNothingThenReturnRhino.Commands.Result.FailureEndIfEndIf' Allow the user to specify the bitmap with in model unitsDim gn AsNewRhino.Input.Custom.GetNumber()
gn.SetCommandPrompt("Bitmap width")
gn.SetLowerLimit(1.0,False)
gn.Get()If gn.CommandResult()<>Rhino.Commands.Result.SuccessThenReturn gn.CommandResult()EndIf' Cook up some scale factorsDim w AsDouble= gn.Number()Dim image_width AsDouble=CDbl(image.Width)Dim image_height AsDouble=CDbl(image.Height)Dim h AsDouble= w *(image_height / image_width)Dim plane AsRhino.Geometry.Plane= view.ActiveViewport.ConstructionPlane()
plane.Origin= gp.Point()
view.ActiveViewport.SetTraceImage(fd.FileName, plane, w, h,False,False)
view.Redraw()ReturnRhino.Commands.Result.SuccessEndFunctionEndClass