Hatch inside of a Region.

Hatch inside of a Region.

nedavo8839
Participant Participant
2,099 Views
8 Replies
Message 1 of 9

Hatch inside of a Region.

nedavo8839
Participant
Participant

Hello again.

I am trying to hatch the newly created region.  I will post some of my tries as comments. Would be very grateful if somebody points me into right diretion, at the moment feels like I am too stuck to get it moving again. The final region might have holes in it.

 

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic; 
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;

public class Commands
{
    [CommandMethod("TurvaAla")]
    public void OffsetLines()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;

        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockTableRecord currentSpace = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;

            // Filter for lines on the "Alusraamistik" layer
            TypedValue[] filterList = new TypedValue[]
            {
                new TypedValue((int)DxfCode.LayerName, "Alusraamistik"),
                new TypedValue((int)DxfCode.Start, "LINE")
            };

            SelectionFilter selFilter = new SelectionFilter(filterList);
            PromptSelectionResult selectionResult = ed.SelectAll(selFilter);

            if (selectionResult.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nNo lines found on the 'Alusraamistik' layer.");
                return;
            }

            SelectionSet selectionSet = selectionResult.Value;

            // Specify the distance for parallel lines
            double offsetDistance = 2.0;

            // Lists to store unique points
            List<Point3d> uniquePoints = new List<Point3d>();

            foreach (ObjectId lineId in selectionSet.GetObjectIds())
            {
                Line originalLine = tr.GetObject(lineId, OpenMode.ForRead) as Line;

                if (originalLine != null)
                {
                    // Get the angle of the original line
                    double angle = originalLine.Angle;

                    // Calculate the offset vectors
                    Vector3d offsetVector1 = new Vector3d(Math.Cos(angle + Math.PI / 2), Math.Sin(angle + Math.PI / 2), 0) * offsetDistance;
                    Vector3d offsetVector2 = new Vector3d(Math.Cos(angle - Math.PI / 2), Math.Sin(angle - Math.PI / 2), 0) * offsetDistance;

                    // Calculate new start and end points for the parallel lines
                    Point3d startPoint1 = originalLine.StartPoint.Add(offsetVector1);
                    Point3d endPoint1 = originalLine.EndPoint.Add(offsetVector1);

                    Point3d startPoint2 = originalLine.StartPoint.Add(offsetVector2);
                    Point3d endPoint2 = originalLine.EndPoint.Add(offsetVector2);

                    // Create new polyline in the "Turvaala" layer
                    Polyline polyline = new Polyline();
                    polyline.AddVertexAt(0, new Point2d(startPoint1.X, startPoint1.Y), 0, 0, 0);
                    polyline.AddVertexAt(1, new Point2d(endPoint1.X, endPoint1.Y), 0, 0, 0);
                    polyline.AddVertexAt(2, new Point2d(endPoint2.X, endPoint2.Y), 0, 0, 0);
                    polyline.AddVertexAt(3, new Point2d(startPoint2.X, startPoint2.Y), 0, 0, 0);
                    polyline.Closed = true;

                    polyline.Layer = "Turvaala";
                    currentSpace.AppendEntity(polyline);
                    tr.AddNewlyCreatedDBObject(polyline, true);

                    // Add unique points to the list
                    AddUniquePoint(uniquePoints, originalLine.StartPoint);
                    AddUniquePoint(uniquePoints, originalLine.EndPoint);
                }
            }

            // Create circles for each unique point
            foreach (Point3d uniquePoint in uniquePoints)
            {
                Circle circle = new Circle(uniquePoint, Vector3d.ZAxis, 2.0);
                circle.Layer = "Turvaala";
                currentSpace.AppendEntity(circle);
                tr.AddNewlyCreatedDBObject(circle, true);
            }

            tr.Commit();

            // Call the ConvertToRegion method
            ConvertToRegion();

            // Delete the original polylines and circles
            DeleteCreatedEntities();

        }
    }

    // Helper method to add unique points to the list
    private void AddUniquePoint(List<Point3d> uniquePoints, Point3d point)
    {
        if (!uniquePoints.Contains(point))
        {
            uniquePoints.Add(point);
        }
    }


    private Region ConvertToRegion()
    {
        Document acDoc = Application.DocumentManager.MdiActiveDocument;
        Database acCurDb = acDoc.Database;

        // Start a transaction
        using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
        {
            // Open the Block table for read
            BlockTable acBlkTbl;
            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

            // Open the Block table record Model space for write
            BlockTableRecord acBlkTblRec;
            acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            // Filter for polylines and circles on the "Turvaala" layer
            TypedValue[] filterList = new TypedValue[]
            {
            new TypedValue((int)DxfCode.LayerName, "Turvaala"),
            new TypedValue((int)DxfCode.Start, "LWPOLYLINE,CIRCLE")
            };

            SelectionFilter selFilter = new SelectionFilter(filterList);
            PromptSelectionResult selectionResult = acDoc.Editor.SelectAll(selFilter);

            if (selectionResult.Status == PromptStatus.OK)
            {
                SelectionSet selectionSet = selectionResult.Value;

                // Create a DBObjectCollection to hold the curves
                DBObjectCollection curveCollection = new DBObjectCollection();

                foreach (ObjectId objectId in selectionSet.GetObjectIds())
                {
                    Entity entity = acTrans.GetObject(objectId, OpenMode.ForRead) as Entity;

                    if (entity != null)
                    {
                        if (entity is Circle || entity is Polyline)
                        {
                            curveCollection.Add(entity);
                        }
                    }
                }

                // Create individual regions from curves
                DBObjectCollection regionCollection = Region.CreateFromCurves(curveCollection);

                // Create a single region from all the regions using Union operation
                Region finalRegion = null;

                foreach (Region region in regionCollection)
                {
                    if (finalRegion == null)
                    {
                        finalRegion = region;
                    }
                    else
                    {
                        // Union operation to combine regions
                        finalRegion.BooleanOperation(BooleanOperationType.BoolUnite, region);
                    }
                }

                if (finalRegion != null)
                {
                    // Append the final region to the Model space
                    finalRegion.Layer = "Turvaala";  // Set the layer to "Turvaala"
                    acBlkTblRec.AppendEntity(finalRegion);
                    acTrans.AddNewlyCreatedDBObject(finalRegion, true);
                }
                else
                {
                    acDoc.Editor.WriteMessage("\nFailed to create a region from the selected curves.");
                }

                // Commit the transaction
                acTrans.Commit();

                // Return the final region
                return finalRegion;
            }

            return null;
        }
    }

    private void DeleteCreatedEntities()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;

        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockTableRecord modelSpace = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;

            // Filter for polylines and circles on the "Turvaala" layer
            TypedValue[] filterList = new TypedValue[]
            {
            new TypedValue((int)DxfCode.LayerName, "Turvaala"),
            new TypedValue((int)DxfCode.Start, "LWPOLYLINE,CIRCLE")
            };

            SelectionFilter selFilter = new SelectionFilter(filterList);
            PromptSelectionResult selectionResult = doc.Editor.SelectAll(selFilter);

            if (selectionResult.Status == PromptStatus.OK)
            {
                SelectionSet selectionSet = selectionResult.Value;

                foreach (ObjectId objectId in selectionSet.GetObjectIds())
                {
                    Entity entity = tr.GetObject(objectId, OpenMode.ForWrite) as Entity;
                    if (entity != null && !entity.IsErased)
                    {
                        entity.Erase();
                    }
                }
            }

            tr.Commit();
        }
    }


}

 

0 Likes
Accepted solutions (1)
2,100 Views
8 Replies
Replies (8)
Message 2 of 9

nedavo8839
Participant
Participant

One of my try-s. I will not post more since they are all very similar.

 

private void CreateHatch(Region finalRegion)
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        // Open the Block table record Model space for write
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

        // Create a hatch and set its properties
        Hatch hatch = new Hatch();
        hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
        hatch.ColorIndex = 1;  // Set your desired color index
        hatch.Transparency = new Transparency(127);

        // Add the hatch loops and complete the hatch
        hatch.Associative = true;

        try
        {
            hatch.AppendLoop(HatchLoopTypes.Default, finalRegion.ObjectId);
        }
        catch (Autodesk.AutoCAD.Runtime.Exception ex)
        {
            // Handle the exception
            doc.Editor.WriteMessage($"\nError during AppendLoop: {ex.Message}");
            return;
        }

        hatch.EvaluateHatch(true);

        // Add the hatch to the modelspace & transaction
        acBlkTblRec.AppendEntity(hatch);
        tr.AddNewlyCreatedDBObject(hatch, true);

        // Commit the transaction
        tr.Commit();
    }
}

 

0 Likes
Message 3 of 9

_gile
Consultant
Consultant
Accepted solution

Hi,

 

It seems we can't use the overload method Hatch.AppendLoop(HatchLoopTypes, ObjectId), which takes an ObjectId as an argument with a region containing multiple loops.

We can use the Hatch.AppendLoop(HatchLoopTypes, Curve2dCollection, IntegerCollection) method, one for each region loop, but this requires each region loop to be converted into a Curve2dCollection collection, which can be done using the region's boundary representation (Brep).

 

That said, you shouldn't use selection sets to get objects you've just created. The program knows the identifiers of these objects to get them directly, and this can be done in the same single transaction. Furthermore, you don't need to add the objects you'll use to create the region to the database and then delete them, just create these objects temporarily "in memory" and call Dispose when you're done with them.

 

Here's an example which seems to work in your case.

 

        [CommandMethod("TEST")]
        public void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var modelSpace = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                var region = CreateRegionFromLines(tr, modelSpace);
                if (region == null) 
                    return;
                HatchRegion(region, tr, modelSpace);
                tr.Commit();
            }
        }

        private static Region CreateRegionFromLines(Transaction tr, BlockTableRecord modelSpace)
        {
            Region finalRegion = null; ;

            // Query for lines on "Alusraamistik" layer
            var lines = modelSpace
                .Cast<ObjectId>()
                .Where(id => id.ObjectClass.DxfName == "LINE")
                .Select(id => (Line)tr.GetObject(id, OpenMode.ForRead))
                .Where(line => line.Layer == "Alusraamistik");

            // Specify the distance for parallel lines
            double offsetDistance = 2.0;

            // HashSet to store unique points
            var uniquePoints = new HashSet<Point3d>();

            // DBObjectCollection to store curves
            var curveSegments = new DBObjectCollection();

            // DBObjectCollection to store regions
            var regions = new DBObjectCollection();

            // Add offset lines and circles to curveSegments
            try
            {
                foreach (var line in lines)
                {
                    var line1 = (Line)line.GetOffsetCurves(offsetDistance)[0];
                    var line2 = (Line)line.GetOffsetCurves(-offsetDistance)[0];
                    curveSegments.Add(line1);
                    curveSegments.Add(line2);
                    curveSegments.Add(new Line(line1.StartPoint, line2.StartPoint));
                    curveSegments.Add(new Line(line2.EndPoint, line1.EndPoint));
                    if (uniquePoints.Add(line.StartPoint))
                        curveSegments.Add(new Circle(line.StartPoint, Vector3d.ZAxis, offsetDistance));
                    if (uniquePoints.Add(line.EndPoint))
                        curveSegments.Add(new Circle(line.EndPoint, Vector3d.ZAxis, offsetDistance));
                }

                // Create the region
                regions = Region.CreateFromCurves(curveSegments);
                finalRegion = regions
                    .Cast<Region>()
                    .Aggregate((r1, r2) =>
                    {
                        r1.BooleanOperation(BooleanOperationType.BoolUnite, r2);
                        r2.Dispose();
                        return r1;
                    });
                finalRegion.Layer = "Turvaala";
                modelSpace.AppendEntity(finalRegion);
                tr.AddNewlyCreatedDBObject(finalRegion, true);
            }

            // Dispose all temporary DBObjects
            finally
            {
                foreach (DBObject curve in curveSegments)
                {
                    curve.Dispose();
                }
            }
            return finalRegion;
        }

        private void HatchRegion(Region region, Transaction tr, BlockTableRecord modelSpace)
        {
            // Create a hatch and set its properties
            Hatch hatch = new Hatch();
            hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
            hatch.ColorIndex = 1;  // Set your desired color index
            hatch.Transparency = new Transparency(127);

            // Add the hatch to the modelspace & transaction
            modelSpace.AppendEntity(hatch);
            tr.AddNewlyCreatedDBObject(hatch, true);

            hatch.Associative = true;

            // Add the hatch loops and complete the hatch
            foreach ((HatchLoopTypes loopType, Curve2dCollection edgePtrs, IntegerCollection edgeTypes) item in GetLoops(region))
            {
                hatch.AppendLoop(item.loopType, item.edgePtrs, item.edgeTypes);
            }

            hatch.EvaluateHatch(true);
        }

        private static IEnumerable<(HatchLoopTypes, Curve2dCollection, IntegerCollection)> GetLoops(Region region)
        {
            var plane = new Plane(Point3d.Origin, region.Normal);
            // Get the region boundary representation
            using (var brep = new Brep(region))
            {
                foreach (var complex in brep.Complexes)
                {
                    foreach (var loop in complex.Shells.First().Faces.First().Loops)
                    {
                        var edgePtrCollection = new Curve2dCollection();
                        var edgeTypeCollection = new IntegerCollection();
                        foreach (var edge in ToOrderedArray(loop.Edges.Select(e => ((ExternalCurve3d)e.Curve).NativeCurve)))
                        {
                            if (edge is CircularArc3d arc)
                            {
                                edgePtrCollection.Add(new CircularArc2d(
                                    arc.StartPoint.Convert2d(plane),
                                    arc.EvaluatePoint((arc.GetParameterOf(arc.StartPoint) + arc.GetParameterOf(arc.EndPoint)) / 2.0).Convert2d(plane),
                                    arc.EndPoint.Convert2d(plane)));
                                edgeTypeCollection.Add(2);
                            }
                            else if (edge is LineSegment3d line)
                            {
                                edgePtrCollection.Add(new LineSegment2d(
                                    line.StartPoint.Convert2d(plane),
                                    line.EndPoint.Convert2d(plane)));
                                edgeTypeCollection.Add(1);
                            }
                        }
                        if (loop.LoopType == LoopType.LoopExterior)
                            yield return (HatchLoopTypes.External, edgePtrCollection, edgeTypeCollection);
                        else
                            yield return (HatchLoopTypes.Default, edgePtrCollection, edgeTypeCollection);
                    }
                }
            }
        }

        public static Curve3d[] ToOrderedArray(IEnumerable<Curve3d> source)
        {
            var list = source.ToList();
            int count = list.Count;
            var array = new Curve3d[count];
            int i = 0;
            array[0] = list[0];
            list.RemoveAt(0);
            int index;
            while (i < count - 1)
            {
                var pt = array[i++].EndPoint;
                if ((index = list.FindIndex(c => c.StartPoint.IsEqualTo(pt))) != -1)
                    array[i] = list[index];
                else if ((index = list.FindIndex(c => c.EndPoint.IsEqualTo(pt))) != -1)
                    array[i] = list[index].GetReverseParameterCurve();
                else
                    throw new ArgumentException("Not contiguous curves.");
                list.RemoveAt(index);
            }
            return array;
        }

 

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 9

nedavo8839
Participant
Participant

Hi!

First very big thanks _gile! Trying to put the code you posted to work, but getting these errors:

1. Error CS0122 'AcAp' is inaccessible due to its protection level TurvaAla line 16- got rid of it replcing the AcAp with Apllication.

But these second 2 I do not know what do to with:
2. Error CS0246 The type or namespace name 'Brep' could not be found (are you missing a using directive or an assembly reference?) line 125
3. Error CS0103 The name 'LoopType' does not exist in the current context line 151

0 Likes
Message 5 of 9

nedavo8839
Participant
Participant
0 Likes
Message 6 of 9

nedavo8839
Participant
Participant

This is the code I ended up with. It is working almost perfectly, I added the hatch.PatternScale. It updates the number, but the actual visual scale stays the same whicever number I put as scale. Should regen after or something? Why the geometry is not updating?

using Autodesk.AutoCAD.BoundaryRepresentation;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.Linq;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;



public class Commands
{
    [CommandMethod("TURVAALA")]
    public void Test()
    {
        var doc = AcAp.DocumentManager.MdiActiveDocument;
        var db = doc.Database;
        var ed = doc.Editor;

        using (var tr = db.TransactionManager.StartTransaction())
        {
            var modelSpace = (BlockTableRecord)tr.GetObject(
                SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
            var region = CreateRegionFromLines(tr, modelSpace);
            if (region == null)
                return;
            HatchRegion(region, tr, modelSpace);
            tr.Commit();
        }
    }

    private static Region CreateRegionFromLines(Transaction tr, BlockTableRecord modelSpace)
    {
        Region finalRegion = null; ;

        // Query for lines on "Alusraamistik" layer
        var lines = modelSpace
            .Cast<ObjectId>()
            .Where(id => id.ObjectClass.DxfName == "LINE")
            .Select(id => (Line)tr.GetObject(id, OpenMode.ForRead))
            .Where(line => line.Layer == "Alusraamistik");

        // Specify the distance for parallel lines
        double offsetDistance = 2.0;

        // HashSet to store unique points
        var uniquePoints = new HashSet<Point3d>();

        // DBObjectCollection to store curves
        var curveSegments = new DBObjectCollection();

        // DBObjectCollection to store regions
        var regions = new DBObjectCollection();

        // Add offset lines and circles to curveSegments
        try
        {
            foreach (var line in lines)
            {
                var line1 = (Line)line.GetOffsetCurves(offsetDistance)[0];
                var line2 = (Line)line.GetOffsetCurves(-offsetDistance)[0];
                curveSegments.Add(line1);
                curveSegments.Add(line2);
                curveSegments.Add(new Line(line1.StartPoint, line2.StartPoint));
                curveSegments.Add(new Line(line2.EndPoint, line1.EndPoint));
                if (uniquePoints.Add(line.StartPoint))
                    curveSegments.Add(new Circle(line.StartPoint, Vector3d.ZAxis, offsetDistance));
                if (uniquePoints.Add(line.EndPoint))
                    curveSegments.Add(new Circle(line.EndPoint, Vector3d.ZAxis, offsetDistance));
            }

            // Create the region
            regions = Region.CreateFromCurves(curveSegments);
            finalRegion = regions
                .Cast<Region>()
                .Aggregate((r1, r2) =>
                {
                    r1.BooleanOperation(BooleanOperationType.BoolUnite, r2);
                    r2.Dispose();
                    return r1;
                });
            finalRegion.Layer = "Turvaala";
            modelSpace.AppendEntity(finalRegion);
            tr.AddNewlyCreatedDBObject(finalRegion, true);
        }

        // Dispose all temporary DBObjects
        finally
        {
            foreach (DBObject curve in curveSegments)
            {
                curve.Dispose();
            }
        }
        return finalRegion;
    }

    private void HatchRegion(Region region, Transaction tr, BlockTableRecord modelSpace)
    {
        // Create a hatch and set its properties
        Hatch hatch = new Hatch();
        hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
        hatch.ColorIndex = 2;  // Set your desired color index
        hatch.Transparency = new Transparency(80);
        // Set the hatch scale (adjust the scale value as needed)
        hatch.PatternScale = 0.05;
        hatch.Layer = "Turvaala";

        // Add the hatch to the modelspace & transaction
        modelSpace.AppendEntity(hatch);
        tr.AddNewlyCreatedDBObject(hatch, true);

        hatch.Associative = true;

        // Add the hatch loops and complete the hatch
        foreach ((HatchLoopTypes loopType, Curve2dCollection edgePtrs, IntegerCollection edgeTypes) item in GetLoops(region))
        {
            hatch.AppendLoop(item.loopType, item.edgePtrs, item.edgeTypes);
        }

        hatch.EvaluateHatch(true);
    }

    private static IEnumerable<(HatchLoopTypes, Curve2dCollection, IntegerCollection)> GetLoops(Region region)
    {
        var plane = new Plane(Point3d.Origin, region.Normal);
        // Get the region boundary representation
        using (var brep = new Brep(region))
        {
            foreach (var complex in brep.Complexes)
            {
                foreach (var loop in complex.Shells.First().Faces.First().Loops)
                {
                    var edgePtrCollection = new Curve2dCollection();
                    var edgeTypeCollection = new IntegerCollection();
                    foreach (var edge in ToOrderedArray(loop.Edges.Select(e => ((ExternalCurve3d)e.Curve).NativeCurve)))
                    {
                        if (edge is CircularArc3d arc)
                        {
                            edgePtrCollection.Add(new CircularArc2d(
                                arc.StartPoint.Convert2d(plane),
                                arc.EvaluatePoint((arc.GetParameterOf(arc.StartPoint) + arc.GetParameterOf(arc.EndPoint)) / 2.0).Convert2d(plane),
                                arc.EndPoint.Convert2d(plane)));
                            edgeTypeCollection.Add(2);
                        }
                        else if (edge is LineSegment3d line)
                        {
                            edgePtrCollection.Add(new LineSegment2d(
                                line.StartPoint.Convert2d(plane),
                                line.EndPoint.Convert2d(plane)));
                            edgeTypeCollection.Add(1);
                        }
                    }
                    if (loop.LoopType == LoopType.LoopExterior)
                        yield return (HatchLoopTypes.External, edgePtrCollection, edgeTypeCollection);
                    else
                        yield return (HatchLoopTypes.Default, edgePtrCollection, edgeTypeCollection);
                }
            }
        }
    }

    public static Curve3d[] ToOrderedArray(IEnumerable<Curve3d> source)
    {
        var list = source.ToList();
        int count = list.Count;
        var array = new Curve3d[count];
        int i = 0;
        array[0] = list[0];
        list.RemoveAt(0);
        int index;
        while (i < count - 1)
        {
            var pt = array[i++].EndPoint;
            if ((index = list.FindIndex(c => c.StartPoint.IsEqualTo(pt))) != -1)
                array[i] = list[index];
            else if ((index = list.FindIndex(c => c.EndPoint.IsEqualTo(pt))) != -1)
                array[i] = list[index].GetReverseParameterCurve();
            else
                throw new ArgumentException("Not contiguous curves.");
            list.RemoveAt(index);
        }
        return array;
    }

}

 

0 Likes
Message 7 of 9

nedavo8839
Participant
Participant
This is the code I ended up with. It is working almost perfectly, I added the hatch.PatternScale. It updates the number, but the actual visual scale stays the same whicever number I put as scale. Should regen after or something? Why the geometry is not updating?

look at the ffinal code in one of the replys to myself. I dont know if the reply to myself comes to your message box or not. Replys and posts can not be deleted here?
0 Likes
Message 8 of 9

_gile
Consultant
Consultant

With Hatches the order you set the properties matters. Try setting the PatternScale before calling SetHatchPattern.

// Create a hatch and set its properties
Hatch hatch = new Hatch();
hatch.ColorIndex = 2;  // Set your desired color index
hatch.Transparency = new Transparency(80);
// Set the hatch scale (adjust the scale value as needed)
hatch.PatternScale = 0.05;
hatch.Layer = "Turvaala";

// Set the hatch pattern
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");

// Add the hatch to the modelspace & transaction
modelSpace.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 9

nedavo8839
Participant
Participant

Just perfect!

Very big thanks!

0 Likes