Code Red > .NET

command "burst" working .net code

(1/2) > >>

nekitip:
Hi all,
I've found some time, and tried to add some functionality to some old plugin that I use.
I've tried to implement burst functionality (as in command "BURST"). I did find some code examples, such as for explode https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID-A853CD73-58A9-4380-B8E2-B3451C94C46B-htm.html and burst (the thing that I'm trying to do) http://adn-cis.org/programmnaya-imitacziya-komandyi-burst.html
However, if that code works, than I've missed some tiny tiny bit somewhere, since I cannot make it work (color remains wrong).

So, I wrote something, and I'm sharing here. The only issue I have, and it's a big one:
-is it safe to copy color the way i did?
I do not see objectid so I know that color is not db resident, but i do see "unmanaged object" pointer (or something). And I'm just recreational developer, so...

--- Code - vb.net: ---Private Shared Sub fillTextFromText(ByRef blockRef As BlockReference, ByRef textFrom As DBText, ByRef textTo As DBText)             textTo.Height = textFrom.Height            textTo.Position = textFrom.Position            textTo.Rotation = textFrom.Rotation            textTo.TextString = textFrom.TextString            textTo.TextStyleId = textFrom.TextStyleId            textTo.WidthFactor = textFrom.WidthFactor            textTo.VerticalMode = textFrom.VerticalMode            textTo.HorizontalMode = textFrom.HorizontalMode            textTo.Normal = textFrom.Normal            textTo.Thickness = textFrom.Thickness            textTo.Oblique = textFrom.Oblique             If Not (textFrom.IsDefaultAlignment) Then                textTo.AlignmentPoint = textFrom.AlignmentPoint            End If             textTo.SetPropertiesFrom(textFrom)         End Sub         Private Shared Sub explodeBlock(btr As BlockTableRecord, blockDef As BlockTableRecord, trans As Transaction, blockRef As BlockReference)            Using explodedObjects As New DBObjectCollection                Dim toAddColl As DBObjectCollection = New DBObjectCollection()                For Each entId As ObjectId In blockDef                    If entId.ObjectClass.Name = "AcDbAttributeDefinition" Then                        Dim attDef As AttributeDefinition = trans.GetObject(entId, OpenMode.ForRead)                         If (attDef.Constant AndAlso Not attDef.Invisible) Then                             If attDef.IsMTextAttributeDefinition Then                                Dim text As MText = New MText()                                text.CopyFrom(attDef.MTextAttributeDefinition) 'no new, so no dispose?                                text.TransformBy(blockRef.BlockTransform)                                toAddColl.Add(text)                            Else                                Dim text As DBText = New DBText()                                fillTextFromText(blockRef, attDef, text)                                text.TransformBy(blockRef.BlockTransform)                                toAddColl.Add(text)                            End If                         End If                    End If                Next                 For Each attRefId As ObjectId In blockRef.AttributeCollection                    Dim attRef As AttributeReference = trans.GetObject(attRefId, OpenMode.ForRead)                     If attRef.Invisible = False Then                         If attRef.IsMTextAttribute Then                            Dim mt As MText = attRef.MTextAttribute 'no new, so no dispose?                            Dim text As MText = New MText()                            text.CopyFrom(mt)                            toAddColl.Add(text)                        Else                            Dim textD As DBText = New DBText()                            fillTextFromText(blockRef, attRef, textD)                            toAddColl.Add(textD)                        End If                    End If                Next                 blockRef.Explode(explodedObjects)                For Each ent As Entity In explodedObjects                    If Not (ent.GetType = GetType(AttributeDefinition)) Then toAddColl.Add(ent)                Next                 For Each ent As Entity In toAddColl                    If ent.Layer = "0" Then                        Dim entColOriginal = ent.Color                        ent.SetPropertiesFrom(blockRef)                        If entColOriginal.ColorIndex > 0 Then ent.Color = entColOriginal                    End If                    'text.TransformBy(blockRef.BlockTransform)                     btr.AppendEntity(ent)                    trans.AddNewlyCreatedDBObject(ent, True)                Next                 'blockRef.UpgradeOpen()                'blockRef.Erase()            End Using         End Sub 

nekitip:
...I've also found this code. https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUID-8ABF5190-C179-4D45-9473-3C78897D352E-htm.html
 It appears to me that color object can be copied with no problem, and, as such, my "burst" .NET code is safe, but... again, I would like to hear other oppinions.

Atook:

--- Quote from: nekitip on January 09, 2019, 03:53:30 AM ---...It appears to me that color object can be copied with no problem, and, as such, my "burst" .NET code is safe, but... again, I would like to hear other oppinions.

--- End quote ---

Hey nekitip, I do something similar, .colorindex is just an int property that can be set. You should be fine as long as the color is between 0 and 256.


--- Code - C#: ---int color;...if (color>=0 && color<=256){  br.ColorIndex = color;}

nekitip:
Atook, thank you for your anwser, that seems well safe, and will cover 99% of all colors.
I will play it a little bit, just to see what happens if there are RGB colors, and all of  bylayer, byblock combinations...

Atook:
for colorIndex, 0=BYBLOCK and 256=BYLAYER.

for RGB colors, you can use Color.FromRgb(23, 54, 232);

There's a nice primer on it here

Navigation

[0] Message Index

[#] Next page

Go to full version