|
| 1 | +using System; |
| 2 | +using Autodesk.AutoCAD.ApplicationServices.Core; |
| 3 | +using Autodesk.AutoCAD.Colors; |
| 4 | +using Autodesk.AutoCAD.DatabaseServices; |
| 5 | +using Autodesk.AutoCAD.EditorInput; |
| 6 | +using Autodesk.AutoCAD.Geometry; |
| 7 | +using Autodesk.AutoCAD.Runtime; |
| 8 | + |
| 9 | +namespace Test; |
| 10 | + |
| 11 | +public class HappyNewYearText |
| 12 | +{ |
| 13 | + [CommandMethod("HappyNewYear")] |
| 14 | + public static void CreateHappyNewYear() |
| 15 | + { |
| 16 | + var doc = Application.DocumentManager.MdiActiveDocument; |
| 17 | + var db = doc.Database; |
| 18 | + var ed = doc.Editor; |
| 19 | + |
| 20 | + // Pick insertion point |
| 21 | + var insRes = ed.GetPoint("\nPick center point for text:"); |
| 22 | + if (insRes.Status != PromptStatus.OK) return; |
| 23 | + |
| 24 | + // Ask for text height |
| 25 | + var hOpts = new PromptDoubleOptions("\nText height:") { AllowNegative = false, AllowZero = false, DefaultValue = 20.0 }; |
| 26 | + var hRes = ed.GetDouble(hOpts); |
| 27 | + if (hRes.Status != PromptStatus.OK) return; |
| 28 | + double textHeight = hRes.Value; |
| 29 | + |
| 30 | + // Ask for confetti count |
| 31 | + var confOpts = new PromptIntegerOptions("\nConfetti count:") { LowerLimit = 0, UpperLimit = 500, DefaultValue = 60 }; |
| 32 | + var confRes = ed.GetInteger(confOpts); |
| 33 | + if (confRes.Status != PromptStatus.OK) return; |
| 34 | + int confettiCount = confRes.Value; |
| 35 | + |
| 36 | + using (var tr = db.TransactionManager.StartTransaction()) |
| 37 | + { |
| 38 | + // Ensure celebration layer exists |
| 39 | + var lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite); |
| 40 | + string layerName = "Celebration"; |
| 41 | + if (!lt.Has(layerName)) |
| 42 | + { |
| 43 | + var ltr = new LayerTableRecord { Name = layerName, Color = Color.FromColorIndex(ColorMethod.ByAci, 6) }; // magenta |
| 44 | + lt.Add(ltr); |
| 45 | + tr.AddNewlyCreatedDBObject(ltr, true); |
| 46 | + } |
| 47 | + db.Clayer = lt[layerName]; |
| 48 | + |
| 49 | + // Ensure text style exists (tries Arial Black; falls back to default) |
| 50 | + var tst = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForWrite); |
| 51 | + string styleName = "HNY_Style"; |
| 52 | + ObjectId styleId; |
| 53 | + if (!tst.Has(styleName)) |
| 54 | + { |
| 55 | + var tsr = new TextStyleTableRecord { Name = styleName, FileName = "Arial Black" }; |
| 56 | + tst.Add(tsr); |
| 57 | + tr.AddNewlyCreatedDBObject(tsr, true); |
| 58 | + styleId = tsr.ObjectId; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + styleId = tst[styleName]; |
| 63 | + } |
| 64 | + |
| 65 | + // Create MText with formatted content |
| 66 | + var mt = new MText |
| 67 | + { |
| 68 | + Location = insRes.Value, |
| 69 | + TextHeight = textHeight, |
| 70 | + Attachment = AttachmentPoint.MiddleCenter, |
| 71 | + TextStyleId = styleId, |
| 72 | + Color = Color.FromColorIndex(ColorMethod.ByAci, 1) // red |
| 73 | + }; |
| 74 | + // Use MText formatting codes: font bold and color changes |
| 75 | + mt.Contents = "\\fArial Black|b1|i0;\\C1;Happy New Year!\\P\\C3;2026"; // red HNY, green year |
| 76 | + |
| 77 | + var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); |
| 78 | + var ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); |
| 79 | + |
| 80 | + ms.AppendEntity(mt); |
| 81 | + tr.AddNewlyCreatedDBObject(mt, true); |
| 82 | + |
| 83 | + // Try to compute extents to place confetti around |
| 84 | + try |
| 85 | + { |
| 86 | + var ext = mt.GeometricExtents; |
| 87 | + var min = ext.MinPoint; |
| 88 | + var max = ext.MaxPoint; |
| 89 | + // Expand bounds for confetti area |
| 90 | + double margin = textHeight * 0.8; |
| 91 | + min = new Point3d(min.X - margin, min.Y - margin, 0); |
| 92 | + max = new Point3d(max.X + margin, max.Y + margin, 0); |
| 93 | + |
| 94 | + var rand = new Random(); |
| 95 | + // Create confetti circles with random colors |
| 96 | + for (int i = 0; i < confettiCount; i++) |
| 97 | + { |
| 98 | + double x = min.X + rand.NextDouble() * (max.X - min.X); |
| 99 | + double y = min.Y + rand.NextDouble() * (max.Y - min.Y); |
| 100 | + double r = Math.Max(0.2, textHeight * (0.03 + 0.05 * rand.NextDouble())); |
| 101 | + var circ = new Circle(new Point3d(x, y, 0), Vector3d.ZAxis, r); |
| 102 | + short aci = (short)new int[] { 1, 2, 3, 4, 5, 6, 140, 30 }[rand.Next(8)]; |
| 103 | + circ.Color = Color.FromColorIndex(ColorMethod.ByAci, aci); |
| 104 | + ms.AppendEntity(circ); |
| 105 | + tr.AddNewlyCreatedDBObject(circ, true); |
| 106 | + } |
| 107 | + } |
| 108 | + catch |
| 109 | + { |
| 110 | + // If extents fail (rare), skip confetti gracefully |
| 111 | + ed.WriteMessage("\nNote: Could not compute text extents for confetti placement."); |
| 112 | + } |
| 113 | + |
| 114 | + tr.Commit(); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments