This article shows how to draw a ball-and-stick model of a simple molecule using Brep entities.
protected int radiusC = 70; protected int radiusH = 53; protected int sp3CH = 220; protected double CH4angle = Utility.DegToRad(109.5);
Brep CHbond = Brep.CreateCylinder(20, sp3CH); Brep H = Brep.CreateSphere(radiusH); H.Translate(0, 0, sp3CH); Brep b1 = Brep.Union(CHbond, H)[0]; b1.Faces[1].Color = Color.DarkGray; b1.Faces[2].Color = Color.Red; Brep C = Brep.CreateSphere(radiusC); Brep b2 = Brep.Union(b1, C)[0]; b2.Faces[2].Color = Color.Black; Brep b3 = (Brep)b1.Clone(); b3.Rotate(CH4angle, Vector3D.AxisX); Brep b4 = Brep.Union(b2, b3)[0]; Brep b5 = (Brep)b3.Clone(); b5.Rotate(Utility.DegToRad(120), Vector3D.AxisZ); Brep b6 = Brep.Union(b4, b5)[0]; Brep b7 = (Brep)b5.Clone(); b7.Rotate(Utility.DegToRad(120), Vector3D.AxisZ); Brep b8 = Brep.Union(b6, b7)[0]; design1.Entities.Add(b8);
design1.Labels.Add(new LeaderAndText(0, 0, 125, "Methane\nfree rotation", new Font("Tahoma", 15f), System.Drawing.Color.Black, new Vector2D(100, 50)));
Plotting a 2D drawing of the model:
// Creating a new sheet
Sheet sheet1 = new Sheet(linearUnitsType.Millimeters, 210, 297, "Sheet 1");
// Adding the sheet to the Sheets collection
drawing1.Sheets.Add(sheet1);
// Setting the current sheet as active
drawing1.ActiveSheet = sheet1;
// Refreshing the control
drawing1.Invalidate();
VectorView vectorViewT = new VectorView(50, 50, viewType.Top, .0001, "Top Vector View");
VectorView vectorViewF = new VectorView(50, 240, viewType.Front, .0001, "Front Vector View");
VectorView vectorViewR = new VectorView(50, 140, viewType.Right, .0001, "Right Vector View");
RasterView rasterView = new RasterView(140, 210, viewType.Trimetric, .00015, "Trimetric Raster View");
RasterView zoomBond = new RasterView(140, 100, new Camera(new Point3D(0, 0, 70), viewType.Trimetric, 10.0), .00015, "Zoom View");
//Adding view's placeholders
sheet1.AddViewPlaceHolder(vectorViewT, design1, drawing1, "Top Vector View");
sheet1.AddViewPlaceHolder(vectorViewF, design1, drawing1, "Front Vector View");
sheet1.AddViewPlaceHolder(vectorViewR, design1, drawing1, "Right Vector View");
sheet1.AddViewPlaceHolder(rasterView, design1, drawing1, "Trimetric Raster View");
AddViewWithHiddenAndDetailsObjects(sheet1);
drawing1.Rebuild(design1);
private void AddViewWithHiddenAndDetailsObjects(Sheet sheet)
{
double extensionAmount = Math.Min(sheet.Width, sheet.Height) / 5;
// Sets the camera to frame the detail
double scale = 0.00040;
double zCoord = 120;
string bondLenght = "108.70 picometers";
Camera cam = new Camera(new Point3D(0, 0, zCoord), 0, design1.ActiveViewport.GetCameraRotation(viewType.Front), projectionType.Orthographic, 0, 1);
RasterView detailView = new RasterView(145, 100, cam, scale, "test", 60, 60);
sheet.AddViewPlaceHolder(detailView, design1, drawing1, detailView.BlockName.Replace(sheet.Name, String.Empty));
// Adds a description
Text detailDescription = new Text(105, 280, "Methane molecular model", 5);
Text angleLabel = new Text(60, 270, "Tetrahedron - 109 degrees angle - Free rotation.", 3);
Text bondLabel = new Text(160, 100, $"{bondLenght}", 3);
detailDescription.Alignment = devDept.Eyeshot.Entities.Text.alignmentType.MiddleCenter;
sheet.Entities.Add(detailDescription);
drawing1.Entities.Add(detailDescription);
drawing1.Entities.Add(angleLabel);
drawing1.Entities.Add(bondLabel);
}
Comments
Please sign in to leave a comment.