Recent Posts

Pages: 1 ... 8 9 [10]
91
.NET / inserting dynamic block by name, or reading dyn props (with example)
« Last post by nekitip on March 25, 2017, 07:41:15 am »
When we talk about blocks, there are a lof of examples online, and here is an example of both reading and writing dynamic block with attributes (by name).
However, my problems are:
-how to read dyn props without crashing (I'm missing something, but I do not know what)
-how to make attributes position themself on right position when we change visuals of blockref from code
-what for are QueueForGraphicsFlush and other commented stuff

Code - vb.net: [Select]
  1. Imports System
  2. Imports Autodesk.AutoCAD.Runtime
  3. Imports Autodesk.AutoCAD.ApplicationServices
  4. Imports Autodesk.AutoCAD.DatabaseServices
  5. Imports Autodesk.AutoCAD.Geometry
  6. Imports Autodesk.AutoCAD.EditorInput
  7.  
  8. <Assembly: CommandClass(GetType(test1.MyCommands))>
  9.  
  10. Namespace test1
  11.  
  12.  
  13.    Public Class MyCommands
  14.  
  15. #Region "commands"
  16.  
  17.        <CommandMethod("INSERTDYNAMICBLOCK")>
  18.        Public Shared Sub insertBlock()
  19.            Dim attList As New List(Of String)
  20.            Dim name As String = "RMNUM"
  21.  
  22.            Dim db As Database = HostApplicationServices.WorkingDatabase()
  23.            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(db)
  24.            Dim ed As Editor = doc.Editor
  25.  
  26.            Using trans As Transaction = db.TransactionManager.StartTransaction()
  27.  
  28.                ' what is this for?
  29.                'doc.TransactionManager.EnableGraphicsFlush(True)
  30.  
  31.                Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
  32.                Dim btr As BlockTableRecord = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
  33.  
  34.                'what is this for?
  35.                'Dim occ As ObjectContextCollection = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES")
  36.  
  37.                Dim blockDef As BlockTableRecord = trans.GetObject(bt(name), OpenMode.ForRead)
  38.                'if bt.has...then
  39.                Using acNewBlockRef As New BlockReference(New Point3d, blockDef.ObjectId)
  40.  
  41.                    btr.AppendEntity(acNewBlockRef)
  42.                    trans.AddNewlyCreatedDBObject(acNewBlockRef, True)
  43.  
  44.  
  45.                    'attributes
  46.                    'classic attrib must always have tag, but tag can be the same
  47.                    'dynamic prop name must be different
  48. 'HERE: BLOCK ATTRIBUTES ARE NOT TRANSFORMED WHEN CHANGING DP (or am I missing something simple somewhere)
  49.                    If acNewBlockRef.IsDynamicBlock Then
  50.                        For Each dp As DynamicBlockReferenceProperty In acNewBlockRef.DynamicBlockReferencePropertyCollection
  51.                            If Not dp.ReadOnly Then
  52.                                'now, IF we change dynprop (flip), then attrib is no longer at proper place without attsync
  53.                                Dim dpnameToSearch As String = "Flip state1"
  54.                                Dim newVal As Double = 1.0
  55.  
  56.                                Select Case dp.PropertyTypeCode
  57.                                    Case 1
  58.                                        dp.Value = newVal
  59.                                    Case 2
  60.                                        dp.Value = Convert.ToInt32(newVal Mod Int32.MaxValue)
  61.                                    Case 3
  62.                                        dp.Value = Convert.ToInt16(newVal Mod Int16.MaxValue)
  63.                                    Case 4
  64.                                        dp.Value = Convert.ToSByte(newVal Mod Byte.MaxValue)
  65.                                    Case 5
  66.                                        dp.Value = Convert.ToString(newVal)
  67.                                    Case 13
  68.                                        dp.Value = Convert.ToInt64(newVal Mod Int64.MaxValue)
  69.                                    Case Else
  70.                                End Select
  71.                            End If
  72.  
  73.                        Next
  74.                    End If
  75.  
  76.  
  77.  
  78.                    For Each attId As ObjectId In blockDef
  79.                        Dim ent As Entity = trans.GetObject(attId, OpenMode.ForRead)
  80.                        If TypeOf ent Is AttributeDefinition Then
  81.                            Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition)
  82.  
  83.                            If (attDef IsNot Nothing) AndAlso (Not attDef.Constant) Then
  84.                                'This is a non-constant AttributeDefinition
  85.                                Using attRef As New AttributeReference()
  86.  
  87.                                    attRef.SetAttributeFromBlock(attDef, acNewBlockRef.BlockTransform)
  88.                                    acNewBlockRef.AttributeCollection.AppendAttribute(attRef)
  89.                                    trans.AddNewlyCreatedDBObject(attRef, True)
  90.  
  91.                                    'CHECK HERE FOR THIS ATTRIB
  92.  
  93.                                    attRef.TextString = "test"
  94.                                    If attRef.HasFields Then
  95.                                        Dim fOif As ObjectId = attRef.GetField()
  96.                                        Dim fo As Field = trans.GetObject(fOif, OpenMode.ForWrite)
  97.                                        fo.Evaluate()
  98.                                    End If
  99.  
  100.                                End Using
  101.                            End If
  102.                        End If
  103.                    Next
  104.  
  105.                    'what is this for?
  106.                    'acNewBlockRef.RecordGraphicsModified(True) ' to force updating a block reference
  107.  
  108.                End Using
  109.  
  110.                'what is this for?
  111.                'trans.TransactionManager.QueueForGraphicsFlush()
  112.  
  113.                trans.Commit()
  114.            End Using
  115.        End Sub
  116.  
  117.        <CommandMethod("GETBLOCKATT")>
  118.        Public Shared Sub getBlockAttributesByBlockName(g)
  119.  
  120.            Dim name As String = "RMNUM"
  121.            Dim realName As String = ""
  122.            realName = name
  123.  
  124.            Dim attList As New List(Of String)
  125.  
  126.            Dim db As Database = HostApplicationServices.WorkingDatabase()
  127.            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(db)
  128.            Dim ed As Editor = doc.Editor
  129.  
  130.            Using trans As Transaction = db.TransactionManager.StartTransaction()
  131.  
  132.                Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
  133.                If bt.Has(name) Then
  134.  
  135.                    Dim blockDef As BlockTableRecord = trans.GetObject(bt(name), OpenMode.ForRead)
  136.                    realName = blockDef.Name
  137.                    If blockDef.HasAttributeDefinitions Then
  138.                        For Each chkObjID As ObjectId In blockDef
  139.                            Dim ent As Entity = trans.GetObject(chkObjID, OpenMode.ForRead)
  140.                            If TypeOf ent Is AttributeDefinition Then
  141.                                Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition)
  142.                                attList.Add(attDef.Tag)
  143.                            End If
  144.                        Next
  145.                    End If
  146.  
  147.                    If blockDef.IsDynamicBlock Then
  148. 'HERE: CRASH IN 50% TIMES
  149.                        'create new temp ref since i do now know how to get all dynprops
  150.                        'here, something is wrong. could be that there are more anonymous block created and they are not deleted with dispose
  151.                        'how to fix this? Or, how to find dynprop withot creating new ref?
  152.                        Dim tempDynBlockRef As New BlockReference(New Point3d, blockDef.ObjectId)
  153.  
  154.                        For Each dp As DynamicBlockReferenceProperty In tempDynBlockRef.DynamicBlockReferencePropertyCollection
  155.                            If Not dp.ReadOnly Then attList.Add(dp.PropertyName)
  156.                        Next
  157.  
  158.                        tempDynBlockRef.Dispose()
  159.  
  160.                    End If
  161.                End If
  162.                trans.Commit()
  163.            End Using
  164.  
  165.            'result is:
  166.            'realname with real name of block
  167.            'attlist with att tag and dynprop names
  168.  
  169.  
  170.        End Sub
  171.  
  172. #End Region
  173.  
  174.    End Class
  175. End Namespace
92
Plotting / Re: Publishing
« Last post by Refri on March 25, 2017, 03:40:26 am »
Pukenzz
We use this solution:
Revers - Automatic batch printing a plurality of frames (format) of the model space and layouts.
http://www.kdmsoft.ru/reverseng.html
93
AutoLISP (Vanilla / Visual) / Re: Routine wanted to modifile
« Last post by kottok on March 24, 2017, 11:55:22 pm »
Hi Peter,

i added a DCL , but the result not satisfied.
94
.NET / Re: Updating MTEXT C#
« Last post by nocent on March 24, 2017, 03:57:54 pm »
Hi Ken,

Method i was having trouble with (note i also had to add the "document lock" to solve the issue production):
Code: [Select]
public static void EditMtextContents(UpdatedTag tag)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;

//create handle from long int
Handle hn = new Handle(ConvertHandleToInt(tag.Handle));

//attempt to get an ObjectID for the handle
ObjectId id = db.GetObjectId(false, hn, 0);

//open the object to edit it
using (DocumentLock doclock = doc.LockDocument())
{
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
DBObject obj = tr.GetObject(id, OpenMode.ForWrite);
//testing editing mtext contents
MText mtext = obj as MText;
mtext.Contents = tag.NewValue;
tr.Commit();
}
}
}

Updated tag is just a small simple class:
Code: [Select]
public class UpdatedTag
{
public string Handle { get; set; }
public string NewValue { get; set; }

public UpdatedTag(string handle, string newValue)
{
Handle = handle;
NewValue = newValue;
}
}

I extract all the mtext and store it in a list/dataset.  part of that information is its "handle" in acad terms. 
i convert the string handle to an autocad object handle using (i believe i got this off from Kean, either way hes been a huge resource):
Code: [Select]
public class UpdatedTag
{
public string Handle { get; set; }
public string NewValue { get; set; }

public UpdatedTag(string handle, string newValue)
{
Handle = handle;
NewValue = newValue;
}
}


Hope this is more helpful than confusing - am very much new to acad and development in general :)
95
.NET / Re: Updating Program Version
« Last post by Atook on March 24, 2017, 03:42:02 pm »
I'm looking in to the same thing. Seems like the plan is to update the main DLL before loading it via a primary 'loader DLL', I don't think a DLL can update it's self.


DGorsman, are you using the System.Reflection.Assembly.LoadFrom() method mentioned here to load the second DLL?
96
CAD General / Re: Copy Paste in Autocad importing other styles.
« Last post by Rob... on March 24, 2017, 03:02:25 pm »
Again, it's bad practice to copy/paste from a "dirty" file to a "clean" file for just this reason. Best to create from scratch.
97
CAD General / Re: Copy Paste in Autocad importing other styles.
« Last post by dubb on March 24, 2017, 02:59:28 pm »
There isn't any dimension associated. I tested copy & paste a simple line from to as clean dwg file and it transferred over styles that I didn't select.
98
.NET / Geometric Constraints
« Last post by nekitip on March 24, 2017, 01:04:25 pm »
I was trying to do a simple thing: insert text or block fixed to the midpoint of curve, horizontal or coplanar. That is possible in AutoCAD without problem, but how to do it programatically?
The only thing "close" to it in entire two hours of googleing was an old post here, written by Jeff H https://www.theswamp.org/index.php?topic=38418.0 I've couldn't find anything else unless it's objarx something...
I've really tried to understand what it does, but I got lost in entire concep...
Any help?

EDIT:
also, running code from that post is not going without problems - cad complaines that "one or more objects in this drawing cannot be saved to the specified format...", both on autosave and on save
99
AutoLISP (Vanilla / Visual) / Re: Replace text with existing block with attribute
« Last post by ronjonp on March 24, 2017, 11:40:34 am »
;D Exacly, I go now, have a nice weekend Ronjonp!
You too  :)
100
Plotting / Re: Publishing
« Last post by Pukenzz on March 24, 2017, 11:35:17 am »
Yes in this case it would be best if I could just batch plot from Model Space but I have been unsuccessful finding the correct solution. 
Pages: 1 ... 8 9 [10]