Author Topic: making Regions in .net  (Read 9176 times)

0 Members and 1 Guest are viewing this topic.

DBARANAS

  • Guest
making Regions in .net
« on: July 06, 2006, 12:07:53 PM »
I am trying to convert a classic VB project into a VB.net project that will run as an in process dll. I hope to lose all the activeX interop stuff and make a true .net app. I am a .net noob and have got as far as making all the forms and IO working. Now it is time to do the hard stuff.


I have searched everywhere for .net examples on how to make a region out of lines that is for passing to into a solid3d so it can be extruded and made into solid. I can make the lines, and making a solid seems easy enough, but making a region out of lines is elusive. You get a createFromCurves method but the create method does not take any parameters...so I am at a loss as to where this seemingly straight forward method is hiding.

The other tough one is how to do the .HandleToObject in .net. So if I have a handle and I want to return an object from it it is quite straight forward in COM but is not clear in .net

These 2 things would get me to 80% and after looking for any example on them and trial and error, I am at a loss. I have been through all the labs, scoured the arx docs.

The sample below works in COM. I do not care about writing the lines or region into the database because they get deleted as soon as the solid gets made.

The one below that is a sorry .net attempt

Any help would be appreciated.

Public Function addRegion(ByRef a, _
                          ByRef b, _
                          ByRef c, _
                          ByRef d) As Variant

    Dim aLin(3) As AcadLine

    With ThisDrawing.ModelSpace
        Set aLin(0) = .AddLine(a, b)
        Set aLin(1) = .AddLine(b, c)
        Set aLin(2) = .AddLine(c, d)
        Set aLin(3) = .AddLine(d, a)
        addRegion = .addRegion(aLin)
    End With

    aLin(0).delete
    aLin(1).delete
    aLin(2).delete
    aLin(3).delete

End Function

    Public Function addRegion(ByRef a As Point3d, _
                          ByRef b As Point3d, _
                          ByRef c As Point3d, _
                          ByRef d As Point3d) As Region

        Dim aLin0 As New Line(a, b)
        Dim aLin1 As New Line(b, c)
        Dim aLin2 As New Line(c, d)
        Dim aLin3 As New Line(d, a)

        addRegion.Create(aLin0, False)

        aLin0.Erase()
        aLin1.Erase()
        aLin2.Erase()
        aLin3.Erase()

    End Function

LE

  • Guest
Re: making Regions in .net
« Reply #1 on: July 06, 2006, 12:24:16 PM »
Welcome!

Have a trip into the arx forum, don't know if the sample there could help or give you a better idea.

http://www.theswamp.org/index.php?topic=9351.0

DBARANAS

  • Guest
Re: making Regions in .net
« Reply #2 on: July 06, 2006, 01:09:43 PM »
Welcome!

Have a trip into the arx forum, don't know if the sample there could help or give you a better idea.

http://www.theswamp.org/index.php?topic=9351.0

Thank You

I checked it out and it also uses the createFromCurves method. All of the solids I make are extruded from rectangular regions.

The only thing even close I saw in the acad.net group where it showed the same method. Maybe I could make a closed rectangular polyline and see if it would extrude that as well. But what a hack that would be! but maybe that is how you have to do it. There is not much of a path to follow in .net so I might as well give it a try.



---------------------------------------------------------
 Dim bb As New DBObjectCollection
Dim cir As New Circle

cir.Center = p0
cir.Radius = 10
bb.Clear()
bb.Add(cir)

Dim regions As DBObjectCollection
regions = Region.CreateFromCurves(bb)
Dim dd1 As Region
dd1 = regions(0)

Dim cc As New Solid3d
cc.ExtrudeAlongPath(dd1, sPline, 0)

btr.AppendEntity(cc)
tm.AddNewlyCreatedDBObject(cc, True)
dd1.Dispose()
----------------------------------------------------------------

LE

  • Guest
Re: making Regions in .net
« Reply #3 on: July 06, 2006, 02:01:34 PM »

DBARANAS

  • Guest
Re: making Regions in .net
« Reply #4 on: July 06, 2006, 02:44:08 PM »
Have a look at this url link:

http://www.autocad.ru/cgi-bin/f1/board.cgi?t=25646zA

Yeah it worked! you can pass a closed polyline and the region takes it. Its that curve word that was misleading.

I cludged this test together and made a solid. The russian URL does it that way too. Now I have to figure out how to bring things back like the .HandleToObject COM way and lots of things will work in .net. I know the handles so there has to be a way in .net for this.

Thanks for helping me

    Public Function DrawSolid() As ObjectId

        Dim bb As New DBObjectCollection
        Dim width As Double = 5
        Dim height As Double = 7
        Dim pt As New Point2d(0, 0)
        Dim plBox As Polyline

        plBox = New Polyline(4)
        plBox.AddVertexAt(0, pt, 0, 0, 0)
        plBox.AddVertexAt(1, New Point2d(pt.X + width, pt.Y), 0, 0, 0)
        plBox.AddVertexAt(2, New Point2d(pt.X + width, pt.Y + height), 0, 0, 0)
        plBox.AddVertexAt(3, New Point2d(pt.X, pt.Y + height), 0, 0, 0)
        plBox.Closed = True

        bb.Clear()
        bb.Add(plBox)

        Dim regions As DBObjectCollection
        regions = Region.CreateFromCurves(bb)
        Dim dd1 As Region
        dd1 = regions(0)

        Dim cc As New Solid3d
        cc.Extrude(dd1, 55, 0)

        Dim lineid As ObjectId
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim tm As DatabaseServices.TransactionManager = db.TransactionManager
        Dim ta As Transaction = tm.StartTransaction()
        Try
            Dim bt As BlockTable = tm.GetObject(db.BlockTableId, DatabaseServices.OpenMode.ForRead, False)
            Dim btr As BlockTableRecord = tm.GetObject(bt(BlockTableRecord.ModelSpace), DatabaseServices.OpenMode.ForWrite, False)
            lineid = btr.AppendEntity(cc)
            tm.AddNewlyCreatedDBObject(cc, True)
            ta.Commit()
        Finally
            ta.Dispose()
            dd1.Dispose()
        End Try
        Return lineid

    End Function

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: making Regions in .net
« Reply #5 on: July 06, 2006, 06:00:50 PM »
A bit late to help but here's an example in C#

Code: [Select]
public void ExtrudePoly(string sectionName, Polyline poly, double height)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
//stuff we need for the solid:
DBObjectCollection colPoly = new DBObjectCollection();
DBObjectCollection colregion = new DBObjectCollection();
Region region = new Region();
Transaction tr = db.TransactionManager.StartTransaction();
try
{
//---- get the block table record to add our new solid to:

BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)as BlockTable;
if(bt == null)
return;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)as BlockTableRecord;
if(btr == null)
return;
//---- create a region from the polyline:
colPoly.Add(poly);
colregion = Region.CreateFromCurves(colPoly);
region = (Region)colregion[0];
//---- create the solid:
Solid3d solid = new Solid3d();
solid.Extrude(region,height,0);
//---- add it to the db:
btr.AppendEntity(solid);
tr.AddNewlyCreatedDBObject(solid,true);
tr.Commit();
}
catch
{
ed.WriteMessage("\nError creating Solid");
}
finally
{
tr.Dispose();
}
}
« Last Edit: July 06, 2006, 07:09:22 PM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

DBARANAS

  • Guest
Re: making Regions in .net
« Reply #6 on: July 06, 2006, 10:16:14 PM »
Thanks for your c# example. I see most people here are using c#. I found this c# to VB.net convertor at http://www.kamalpatel.net that does a nice job so I just translate it over and its always at least 95% or perfect.

I took your example the broke it into 3 subs. The addSomething one is the one that get's plugged in everywhere and the other 2 just do their jobs.

I changed the poly to a 3D poly so solids can be extruded from a known 3D point. Everything I do is in the WCS. I have seen some posts over in the acad.net group mentioning that there is no 3DRotate in .net. I don't see anything in the object browser except in Interop. Although there is a method in DOTNETARX for it. The same thing holds true for MOVE

If it can't be done then maybe this would be the place to do the rotation and move before the solid gets made.

  Public Sub addSomething()

        'some hard coded stuff for testing
        Dim elevation As Double = 33.25
        Dim length As Double = 1.5
        Dim width As Double = 5.5
        Dim height As Double = 92.25

        Dim a As New Point3d(0, 0, elevation)
        Dim b As New Point3d(length, 0, elevation)
        Dim c As New Point3d(length, width, elevation)
        Dim d As New Point3d(0, width, elevation)

        drawPoly(a, b, c, d, height)

    End Sub
    Public Sub drawPoly(ByVal a As Point3d, _
                             ByVal b As Point3d, _
                             ByVal c As Point3d, _
                             ByVal d As Point3d, _
                             ByVal height As Double)

        Dim bb As New DBObjectCollection
        Dim pntCol As New Point3dCollection

        pntCol.Add(a)
        pntCol.Add(b)
        pntCol.Add(c)
        pntCol.Add(d)

        Dim plBox = New Polyline3d(Poly3dType.SimplePoly, pntCol, True)

        extrudePoly(plBox, height)
        plBox.dispose()

    End Sub
    Public Sub extrudePoly(ByVal poly As Polyline3d, _
                           ByVal height As Double)

        Dim colregion As DBObjectCollection = New DBObjectCollection()
        Dim colPoly As DBObjectCollection = New DBObjectCollection()
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim region As Region = New Region()
        Dim tm As DatabaseServices.TransactionManager = db.TransactionManager
        Dim tr As Transaction = tm.StartTransaction

        Try
            'get the block table record to add our new solid to
            Dim bt As BlockTable = tm.GetObject(db.BlockTableId, DatabaseServices.OpenMode.ForRead, False)
            If bt Is Nothing Then Return
            Dim btr As BlockTableRecord = tm.GetObject(bt(BlockTableRecord.ModelSpace), DatabaseServices.OpenMode.ForWrite, False)
            If btr Is Nothing Then Return
            'create a region from the polyline
            colPoly.Add(poly)
            colregion = region.CreateFromCurves(colPoly)
            region = CType(colregion(0), Region)
            'create the solid
            Dim solid As Solid3d = New Solid3d()
            solid.Extrude(region, height, 0)
            'add it to the db
            btr.AppendEntity(solid)
            tr.AddNewlyCreatedDBObject(solid, True)
            tr.Commit()
        Catch
            ed.WriteMessage("\nError creating Solid")
        Finally
            tr.Dispose()
        End Try

    End Sub

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: making Regions in .net
« Reply #7 on: July 06, 2006, 10:42:07 PM »
Generally, I would create ALL objects based around the wcs. This way you can insert blocks etc and then move them to where ever and facing any which way you like. The code I posted just creates an extruded solid, the user of this solid is responsible for putting it in the right place, this also makes it more flexible.
While .net doesn't have move or rotate as such, you can do these things and more with matrices (this is what happens behind the scenes with acad anyway). The trick here is to get the matrices in the correct order ie. build a mtrix to translate (move) before creating a matrix to rotate (Matrix3d.Rotation()). You can even combine these 2 matrices by using Post/Pre multiplyBy to make one matrix for your transformations of multiple objects say.

Cheers,
Mick.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: making Regions in .net
« Reply #8 on: July 08, 2006, 02:51:24 AM »
Have a look at this url link:

http://www.autocad.ru/cgi-bin/f1/board.cgi?t=25646zA
:-D Luis! I hope that it is not need to study Russian language in order to understand this code.

LE

  • Guest
Re: making Regions in .net
« Reply #9 on: July 08, 2006, 10:28:44 AM »
Have a look at this url link:

http://www.autocad.ru/cgi-bin/f1/board.cgi?t=25646zA
:-D Luis! I hope that it is not need to study Russian language in order to understand this code.

 :-)

There is a lot of info over there..... and Это могла быть хорошая идея, изучать русский язык at the same time ...