.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Common function for a namespace

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
parikhnidi
586 Views, 6 Replies

Common function for a namespace

Hi,

 

Is there a way to define a private function that is accessible to all classes within that namespace? I have developed many classes within the namespace and each class (object) has a Draw() method to draw that object.

 

What I noticed that, within the Draw() method I will need to call a common function with the following signature. It returns void.

CreateObject(Point2dCollection pPoints, Vector3d pDirection, Vector3d pDisplacement)

 

This would help me in debugging the code at one place only.

 

Nimish

6 REPLIES 6
Message 2 of 7
_gile
in reply to: parikhnidi

Hi,

A method have to be declared in a class, a structure or an interface.

You should show more code, so that's easier to understand your issue.

According to your description, the OOP way should be that all your classes derive from an abstract class in which the CreateOject is defined.

Another way to make a method easily accessible for all classes of a namespace is to make a static method in an Helpers class.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 7
parikhnidi
in reply to: _gile

Sorry for getting back late. I will get you the code I am using over the weekend.

 

Thanks,

Nimish

Message 4 of 7
parikhnidi
in reply to: _gile

Hi @_gile 

 

Here is the code for one of the class within the namespace. As you can see from the code below -

 

I am executing following steps that are common to most of the classes.

 

  1.  Create a Polyline.
  2. Add vertices to this Polyline from Point2dCollection parameter.
  3. Add newly created Polyline to DBObjectCollection variable.
  4. Create region.
  5. Revolve region object about Y-axis 360°.
  6. Align solid achieved by revolution above to suit desired Direction parameter.
  7. Move object using Displacement parameter.

I am trying to conceive a function something like a following signature -

RevolveAndAlineObject(Point2dCollection Polypoints, Vector3d Direction, Vector Displacement)

 

I've written plenty of drafting helper functions in an independent class called DrawingHelper. But, I was thinking to use proposed function within the namespace specified.

 

Is there a way to create function that will be exclusively used by the classes within the namespace.

 

Thanks,

Nimish

 

public class Shell3d
{
    private double _InsideDiameter, _Thickness, _Length;

    private Point3d _Location;
    private Vector3d _Direction;

    private readonly Point2dCollection ShellPoints = new Point2dCollection();

    private Point3d Origin3d = new Point3d(0, 0, 0);
    private Point2d Origin2d = new Point2d();
    private Vector3d DisplacementVector;
    public double InsideDiameter
    {
        set { _InsideDiameter = value; }
        get { return _InsideDiameter; }
    }
    public double Thickness
    {
        set { _Thickness = value; }
        get { return _Thickness; }
    }
    public double Length
    {
        set { _Length = value; }
        get { return _Length; }
    }
    public Point3d Location
    {
        set { _Location = value; }
        get { return _Location; }
    }
    public Vector3d Direction
    {
        set { _Direction = value; }
        get { return _Direction; }
    }
    private void CalculateProperties()
    {
        Origin2d = Origin3d.Convert2d(new Plane(Origin3d, Vector3d.ZAxis));
        DisplacementVector = Origin3d.GetVectorTo(_Location);
    }
    private void CalculateVertices()
    {

        Point2d tempPoint2d;

        CalculateProperties();
        ShellPoints.Clear();

        tempPoint2d = Origin2d.Add(new Vector2d(_InsideDiameter / 2, _Length / 2));
        ShellPoints.Add(tempPoint2d);

        tempPoint2d = tempPoint2d.Add(new Vector2d(_Thickness, 0));
        ShellPoints.Add(tempPoint2d);

        tempPoint2d = tempPoint2d.Add(new Vector2d(0, -_Length));
        ShellPoints.Add(tempPoint2d);

        tempPoint2d = tempPoint2d.Add(new Vector2d(-Thickness, 0));
        ShellPoints.Add(tempPoint2d);
    }
    public void Draw()
    {
        Document acaddoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        Database acaddb = acaddoc.Database;
        //Editor edt = acaddoc.Editor;

        CalculateVertices();

        int VertexIndex = 0;

        Polyline ShellPolyLine = new Polyline();

        foreach (Point2d ShellPoint in ShellPoints)
        {
            ShellPolyLine.AddVertexAt(VertexIndex, ShellPoint, 0, 0, 0);
            VertexIndex += 1;
        }
        ShellPolyLine.Closed = true;

        DBObjectCollection pl = new DBObjectCollection();
        pl.Add(ShellPolyLine);

        DBObjectCollection regn = Region.CreateFromCurves(pl);
        Region region = (Region)regn[0];

        Solid3d Shell = new Solid3d();
        Shell.Revolve(region, Origin3d, new Vector3d(0, 1, 0), 2 * PI);

        if (_Direction.X == 0 && _Direction.Y == 0)
        {
            Shell.TransformBy(Matrix3d.Rotation(PI / 2, (_Direction.Z > 0 ? 1 : -1) * Vector3d.XAxis, Origin3d));

        }
        else
        {
            Vector3d XYVector = _Direction.OrthoProjectTo(Vector3d.ZAxis);

            double rotation = Vector3d.YAxis.GetAngleTo(XYVector, Vector3d.ZAxis);
            Shell.TransformBy(Matrix3d.Rotation(rotation, Vector3d.ZAxis, Origin3d));
            Vector3d RotAxis = XYVector.GetPerpendicularVector().Negate();
            rotation = XYVector.GetAngleTo(_Direction, RotAxis);

            Shell.TransformBy(Matrix3d.Rotation(rotation, RotAxis, Origin3d));
        }
        Shell.TransformBy(Matrix3d.Displacement(DisplacementVector));

        using (Transaction trans = acaddb.TransactionManager.StartTransaction())
        {
            BlockTable bt;
            bt = trans.GetObject(acaddb.BlockTableId, OpenMode.ForRead) as BlockTable;

            BlockTableRecord btr;
            btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            btr.AppendEntity(Shell);
            trans.AddNewlyCreatedDBObject(Shell, true);

            trans.Commit();
        }

    }

}//Class - Shell3d

 

 

Message 5 of 7
_gile
in reply to: parikhnidi

I'm not able to fully understand what you want to achieve but if your different classes have common methods and properties, you should use inheritance.
You create an abstract base class which contains the common methods and properties and the abstract members required by the common methods. Then make each class deriving from the base class.

See this topic:

https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/object-oriented/inheritance



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 7
parikhnidi
in reply to: _gile

Thanks a lot @_gile 

 

It works great. I was able to compact the code substantially. Here is the finished product.

 

Nimish

 

Base class:

 

    public abstract class RevolvedObject
    {
        protected double _InsideDiameter, _Thickness;
        protected Point3d _Location;
        protected Vector3d _Direction;

        protected Point2dCollection _ProfilePoints = new Point2dCollection();

        protected Point3d Origin3d = new Point3d(0, 0, 0);
        protected Point2d Origin2d = new Point2d(0, 0);
        private Vector3d DisplacementVector;

        public double InsideDiameter
        {
            set { _InsideDiameter = value; }
            get { return _InsideDiameter; }
        }
        public double Thickness
        {
            set { _Thickness = value; }
            get { return _Thickness; }
        }
        public Point3d Location
        {
            set { _Location = value; }
            get { return _Location; }
        }
        public Vector3d Direction
        {
            set { _Direction = value; }
            get { return _Direction; }
        }
        public void Draw()
        {
            Document acaddoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acaddb = acaddoc.Database;

            int VertexIndex = 0;

            DisplacementVector = Origin3d.GetVectorTo(_Location);

            Polyline ProfilePolyLine = new Polyline();

            foreach (Point2d _ProfilePoint in _ProfilePoints)
            {
                ProfilePolyLine.AddVertexAt(VertexIndex, _ProfilePoint, 0, 0, 0);
                VertexIndex += 1;
            }
            ProfilePolyLine.Closed = true;

            DBObjectCollection pl = new DBObjectCollection();
            pl.Add(ProfilePolyLine);

            DBObjectCollection regn = Region.CreateFromCurves(pl);
            Region region = (Region)regn[0];

            Solid3d RevolvedObject = new Solid3d();
            RevolvedObject.Revolve(region, Origin3d, new Vector3d(0, 1, 0), 2 * PI);

            if (_Direction.X == 0 && _Direction.Y == 0)
                RevolvedObject.TransformBy(Matrix3d.Rotation(PI / 2, ((_Direction.Z > 0) ? 1 : -1) * Vector3d.XAxis, Origin3d));
            else
            {
                Vector3d XYVector = _Direction.OrthoProjectTo(Vector3d.ZAxis);

                double rotation = Vector3d.YAxis.GetAngleTo(XYVector, Vector3d.ZAxis);
                RevolvedObject.TransformBy(Matrix3d.Rotation(rotation, Vector3d.ZAxis, Origin3d));
                Vector3d RotAxis = XYVector.GetPerpendicularVector().Negate();
                rotation = XYVector.GetAngleTo(_Direction, RotAxis);

                RevolvedObject.TransformBy(Matrix3d.Rotation(rotation, RotAxis, Origin3d));
            }
            RevolvedObject.TransformBy(Matrix3d.Displacement(DisplacementVector));

            using (Transaction trans = acaddb.TransactionManager.StartTransaction())
            {
                BlockTable bt;
                bt = trans.GetObject(acaddb.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr;
                btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                btr.AppendEntity(RevolvedObject);
                trans.AddNewlyCreatedDBObject(RevolvedObject, true);

                trans.Commit();
            }
        }
    }

Inherited class

public class Shell3d : RevolvedObject
    {
        private double _Length;
        public double Length
        {
            set { _Length = value; }
            get { return _Length; }
        }
        private void CalculateVertices()
        {
            Point2d tempPoint;

            _ProfilePoints.Clear();

            tempPoint = Origin2d.Add(new Vector2d(_InsideDiameter / 2, _Length / 2));
            _ProfilePoints.Add(tempPoint);

            tempPoint = tempPoint.Add(new Vector2d(_Thickness, 0));
            _ProfilePoints.Add(tempPoint);

            tempPoint = tempPoint.Add(new Vector2d(0, -_Length));
            _ProfilePoints.Add(tempPoint);

            tempPoint = tempPoint.Add(new Vector2d(-Thickness, 0));
            _ProfilePoints.Add(tempPoint);
        }
        public new void Draw()
        {
            CalculateVertices();
            base.Draw();
        }
    }

 

Message 7 of 7
_gile
in reply to: parikhnidi

You should not have to redefine tke Draw method in the derived classes.

 

In the RevolvedObject class, you can declare the CalculateVertices method as abstract:

protected abstract void CalculateVertices();

and call this method in the Draw method to fill the _Profilepoints field*.

 

In each derived classes you'll have to define an overriden CalculateVertices method.

protected override void CalculteVertices()
{
    // here the code for each specific class
}

 

* by my side, I won't use methods which return void and fill a field by  a side effect. I'd rather use methods with explicit return values (e.g. a Point3dCollection for CalculateVetices).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report