private static bool AddHatch(Point2dCollection boundaryPoints, ObjectId fflBlockId, bool isExposed) { Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument; Database acCurrDb = acDoc.Database; Editor acEditor = acDoc.Editor; // Lose this line in the real command bool success = JPPCommandsInitialisation.setJPPLayers(); using (Transaction acTrans = acCurrDb.TransactionManager.StartTransaction()) { try { Polyline hatchBoundary = new Polyline(); // need to transform the outline by the block reference transform BlockReference fflBlock = acTrans.GetObject(fflBlockId, OpenMode.ForRead) as BlockReference; for (int index = 0; index < boundaryPoints.Count; index++) { Point3d nextVertex = new Point3d(boundaryPoints[index].X, boundaryPoints[index].Y, 0.0).TransformBy(fflBlock.BlockTransform); hatchBoundary.AddVertexAt(index, new Point2d(nextVertex.X, nextVertex.Y), 0, 0, 0); } hatchBoundary.Closed = true; if (isExposed) { hatchBoundary.Layer = StyleNames.JPP_App_Exposed_Brick_Layer; } else { hatchBoundary.Layer = StyleNames.JPP_App_Tanking_Layer; } // Add the hatch boundary to modelspace BlockTable acBlkTbl = acTrans.GetObject(acCurrDb.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; acBlkTblRec.AppendEntity(hatchBoundary); acTrans.AddNewlyCreatedDBObject(hatchBoundary, true); // Add the hatch boundry to an object Id collection ObjectIdCollection acObjIdColl = new ObjectIdCollection(); acObjIdColl.Add(hatchBoundary.Id); // Set the hatch properties using (Hatch exposedHatch = new Hatch()) { acBlkTblRec.AppendEntity(exposedHatch); acTrans.AddNewlyCreatedDBObject(exposedHatch, true); // Set the hatch properties exposedHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31"); if (isExposed) { exposedHatch.Layer = StyleNames.JPP_App_Exposed_Brick_Layer; exposedHatch.BackgroundColor = Color.FromColorIndex(ColorMethod.ByAci, 80); } else { exposedHatch.Layer = StyleNames.JPP_App_Tanking_Layer; exposedHatch.BackgroundColor = Color.FromColorIndex(ColorMethod.ByAci, 130); } exposedHatch.PatternScale = 0.1; exposedHatch.PatternSpace = 0.1; exposedHatch.PatternAngle = Constants.Deg_45; exposedHatch.Associative = true; exposedHatch.Annotative = AnnotativeStates.False; exposedHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl); exposedHatch.EvaluateHatch(true); } acTrans.Commit(); return(true); } catch (Autodesk.AutoCAD.Runtime.Exception acException) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog ("The following exception was caught: \n" + acException.Message + "\nError adding hatch!\n"); acTrans.Commit(); return(false); } } }
public void FFL() { // Get the current document Document acDoc = Application.DocumentManager.MdiActiveDocument; Editor acEditor = acDoc.Editor; // Save the current text style string currTextStyle = System.Convert.ToString(Application.GetSystemVariable("TextStyle")); // Initialise if (!JPPCommandsInitialisation.JPPCommandsInitialise()) { acEditor.WriteMessage("\nUnable to initialise the FFL command!"); return; } // Will need to save other elements of current context, e.g. layer // to make sure current state is restored at when the command is complete // Show the FFL command on the command-line // Loop until valid command input or ESC pressed bool ValidCommand = false; // string SubCommand = ""; while (ValidCommand == false) { // Display the command and get the FFL sub-command PromptResult SubCommand = acEditor.GetString("\n Add, Delete, Edit, Exposed Brickwork, eXit: "); // Check if the ESC key has pressed if (SubCommand.Status == PromptStatus.Cancel) { acEditor.WriteMessage("ESC key pressed!\n"); ValidCommand = true; } else if (SubCommand.Status == PromptStatus.OK) { // A string has been returned so convert to lower case and check // sub -command is valid. // Note that a valid sub-command can be a single letter abbreviation..... // // "A" or "a" = Add // "D" or "d" = Delete // "E" or "e" = Edit // "B" or "b" = Exposed Brickwork/Tanking // "X" or "x" = Exit // // // SubCommand = ToLower(PromptReturnString.ToString); switch (SubCommand.StringResult.ToLower()) { case "add": case "a": if (!AddFFL.NewFFL()) { acEditor.WriteMessage("\nUnable to add new outline - exiting FFL command!"); ValidCommand = true; } break; case "delete": case "d": acEditor.WriteMessage("FFL Delete command!\n"); ValidCommand = true; break; case "edit": case "e": if (!EditFFL.EditFFLOrLevels()) { acEditor.WriteMessage("\nUnable to edit FFL or Levels - exiting FFL command!"); ValidCommand = true; } break; case "brickwork": case "b": if (!AddBrickwork.ExposedAndTanking()) { acEditor.WriteMessage("\nUnable to add exposed brick and/or tanking!"); ValidCommand = true; } break; case "exit": case "x": acEditor.WriteMessage("FFL Exit command!\n"); ValidCommand = true; break; default: acEditor.WriteMessage("Invalid command specified!\n"); //validCommand = true; break; } } } // Restore current context Application.SetSystemVariable("TEXTSTYLE", currTextStyle); acEditor.WriteMessage("\nFFL command finished."); }