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

How To Make block using vb.net

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
3711 Views, 4 Replies

How To Make block using vb.net

Hi All,

 

Suppose a make a drawing containing lines and arcs using vb.net. Now how can i combines   thses lines and arcs to make one block .

 

I also want to know, if above block is a closed figure , then how can i fill this closed block by hashed line.

 

Please help

 

Regards

Amit

4 REPLIES 4
Message 2 of 5
gizmowiebe
in reply to: Anonymous

Hi Amit,

 

you can do that by creating a selectionset and add the lines and arc to this selectionset. Check also

 

http://www.visiblevisual.com/index.php/AutoCad-VBA/createblock.html

 

regards

 

Wiebe

Message 3 of 5
Hallex
in reply to: Anonymous

This will get you started

static public void MakeBlockFromSS()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Transaction tr = db.TransactionManager.StartTransaction();

            Editor ed = doc.Editor;

            using (tr)
            {
                try
                {
                    SelectionFilter sf = new SelectionFilter(

                        new TypedValue[] { new TypedValue(0, "LINE,ARC") }

                            );
                    PromptSelectionOptions pso = new PromptSelectionOptions();

                    pso.MessageForAdding = "\nSelect arc and lines >>";

                    PromptSelectionResult res = ed.GetSelection(pso, sf);

                    if (res.Status != PromptStatus.OK)

                        return;

                    SelectionSet main = res.Value;

                    if (main.Count==0)

                        return;

                    ObjectIdCollection oids = new ObjectIdCollection(main.GetObjectIds());


                    ObjectId idx = AddHatch(oids, "ANSI37", 20.0, 0.0);

                    if (idx == ObjectId.Null)
                    {
                        ed.WriteMessage("\nProblem with creating boundary\ncheck gaps between objects\nProgram exiting");

                        return;
                    }

                    oids.Add(idx);

                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    PromptStringOptions psto =  new PromptStringOptions( "\nEnter new block name: "  );

                    psto.AllowSpaces = true;

                    string blkname = "";

                    do
                    {

                        PromptResult pr = ed.GetString(psto);

                        if (pr.Status != PromptStatus.OK)

                            return;

                        try
                        {

                            SymbolUtilityServices.ValidateSymbolName(  pr.StringResult, false );


                            if (bt.Has(pr.StringResult))

                                ed.WriteMessage( "\nA layer with this name already exists."  );

                            else

                                blkname = pr.StringResult;

                        }

                        catch
                        {
                            ed.WriteMessage(  "\nInvalid block name." );

                        }
                    } while (blkname == "");

                    BlockTableRecord btr = new BlockTableRecord();

                    PromptPointOptions ppo = new PromptPointOptions("\nPick Insertion Point >>");

                    ppo.UseBasePoint = false;

                    PromptPointResult pres;

                    pres = ed.GetPoint(ppo);

                    if (pres.Status != PromptStatus.OK)
                        return;

                    btr.Origin = pres.Value;

                    btr.Name = blkname;

                    btr.Units = UnitsValue.Millimeters;

                    btr.Annotative = AnnotativeStates.True;

                    bt.UpgradeOpen();

                    ObjectId ltId = bt.Add(btr);

                    bt.DowngradeOpen();

                    tr.AddNewlyCreatedDBObject(btr, true);

           
                    btr.AssumeOwnershipOf(oids);

                    tr.Commit();

                }

                catch (System.Exception ex)
                {
                    ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace);

                    return;
                }

            }
        }

        static public ObjectId AddHatch(ObjectIdCollection ids, string pattern, double scale, double angle)
        {

            ObjectId idx =ObjectId.Null;

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {
                try
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                    Hatch hatch = new Hatch();

                    btr.AppendEntity(hatch);

                    tr.AddNewlyCreatedDBObject(hatch, true);

                    hatch.SetDatabaseDefaults();

                    hatch.SetHatchPattern(HatchPatternType.PreDefined, pattern);

                    hatch.PatternScale = scale;

                    hatch.PatternAngle = Math.PI * angle / 180.0;

                    hatch.Associative = true;

                    hatch.Annotative = AnnotativeStates.True;

                    hatch.AppendLoop(HatchLoopTypes.Outermost, ids);

                    hatch.EvaluateHatch(true);

                    idx = hatch.ObjectId;

                    tr.Commit();
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    ed.WriteMessage(ex.Message);

                    idx = ObjectId.Null;
                
                }
            }
            return idx;
        }

 

Change to your suit

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 5
marolek
in reply to: Hallex

Hi,

i would like to write code to make and insert block using collection of ObjectId. I try do this basing on the hallex's code and now i have some like this:

 

Public Function zrob_blok(ByVal OBidy As ObjectIdCollection) As Boolean
  Dim oDWG As Document = Application.DocumentManager.MdiActiveDocument
  Dim oDB As Database = oDWG.Database
  Dim oTrans As DatabaseServices.Transaction = oDB.TransactionManager.StartTransaction
  Dim oBT As BlockTable = CType(oTrans.GetObject(oDB.BlockTableId, OpenMode.ForWrite), BlockTable)
  Dim oBTR As BlockTableRecord = New BlockTableRecord
  Dim retVal As Boolean = False oBTR.Origin = pktOtworu 'pktOtworu this is Public variable Geometry.Point3d
oBTR.Name = nazwaTXT.Text 'nazwaTXT - control on Form
        Try oBT.UpgradeOpen() Dim objekt As ObjectId = oBT.Add(oBTR) oBT.DowngradeOpen() oTrans.AddNewlyCreatedDBObject(oBTR, True) oBTR.AssumeOwnershipOf(OBidy) oTrans.Commit() Catch ex As System.Exception MsgBox(ex.Message, MsgBoxStyle.Information, "Error in zrob_blok") Finally oTrans.Dispose() End Try Return retVal End Function

but i have a problem bacause this work in a half

atfer this procedure block is created but not inserted to drawing,

 

any ideas to fix this problem?

 

Please help

 

Regards

marolek

Message 5 of 5
Hallex
in reply to: marolek

Have you tried to use 'Search' button at upper right of main page?

See a lot of threads here:

http://forums.autodesk.com/t5/forums/searchpage/tab/message?filter=location&location=Board%3A152&q=i...

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost