Jump to content

Recommended Posts

Posted

Hi all, Trying to convert the popular VB VPlayerOff function to VB.Net with added functionality of turning a layer off in all viewports in a layout. My code seems to do everything right except update the Xdata with the new results. I tried manipulating the Xdata result buffer initially but is said it was read only so that is why I have done it this way.

 

I will post the Xdata functions from "From Jerry Winters AU 2009 Class - "Store it in the DWG: XData, Extension Dictionaries and Object Data Through .NET" in a separate post since I went over my allotted 15000 charaters.

 

Note, Current code needs an object on a layer called 'test' in drawing file, with something in the layer :).

 

Thanks

Mike

 

PS - first time posting so hope I did this right!

 

[size=3][font=Times New Roman][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.Interop[/font][/font][/size]
[font=Times New Roman][size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.Interop.Common[/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.Runtime[/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD[/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.DatabaseServices[/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.EditorInput[/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.Geometry[/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.ApplicationServices[/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.ApplicationServices.[color=#2b91af]Application[/color][/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.LayerManager[/font][/size]
[size=3][color=blue][font=Consolas]Imports[/font][/color][font=Consolas] Autodesk.AutoCAD.Windows[/font][/size]

[size=3][color=blue][font=Consolas]Public[/font][/color][font=Consolas] [color=blue]Class[/color] [color=#2b91af]vbTools[/color][/font][/size]
[size=3][font=Consolas]<[color=#2b91af]CommandMethod[/color]([color=#a31515]"VPLOFF"[/color])> _[/font][/size]
[size=3][color=blue][font=Consolas]Public[/font][/color][font=Consolas] [color=blue]Sub[/color] VPLOFF()[/font][/size]
[size=3][color=blue][font=Consolas]Dim[/font][/color][font=Consolas] myDB [color=blue]As[/color] [color=#2b91af]Database[/color] = [color=#2b91af]HostApplicationServices[/color].WorkingDatabase[/font][/size]
[size=3][color=blue][font=Consolas]Using[/font][/color][font=Consolas] myTrans [color=blue]As[/color] [color=#2b91af]Transaction[/color] = myDB.TransactionManager.StartTransaction[/font][/size]
[size=3][color=blue][font=Consolas]Dim[/font][/color][font=Consolas] myBT [color=blue]As[/color] [color=#2b91af]BlockTable[/color] = myDB.BlockTableId.GetObject([color=#2b91af]OpenMode[/color].ForWrite)[/font][/size]
[size=3][color=blue][font=Consolas]For[/font][/color][font=Consolas] [color=blue]Each[/color] myBtrID [color=blue]As[/color] [color=#2b91af]ObjectId[/color] [color=blue]In[/color] myBT[/font][/size]
[size=3][color=blue][font=Consolas]  Dim[/font][/color][font=Consolas] myBTR [color=blue]As[/color] [color=#2b91af]BlockTableRecord[/color] = myBtrID.GetObject([color=#2b91af]OpenMode[/color].ForWrite)[/font][/size]
[size=3][color=blue][font=Consolas]  If[/font][/color][font=Consolas] myBTR.IsLayout = [color=blue]True[/color] [color=blue]Then[/color][/font][/size]
[size=3][color=blue][font=Consolas]    Dim[/font][/color][font=Consolas] myLayout [color=blue]As[/color] [color=#2b91af]Layout[/color] = myBTR.LayoutId.GetObject([color=#2b91af]OpenMode[/color].ForWrite)[/font][/size]
[size=3][color=blue][font=Consolas]    Dim[/font][/color][font=Consolas] myLayoutString [color=blue]As[/color] [color=blue]String[/color] = myLayout.LayoutName[/font][/size]
[size=3][color=blue][font=Consolas]    Dim[/font][/color][font=Consolas] myVPIDs [color=blue]As[/color] [color=#2b91af]ObjectIdCollection[/color] = myLayout.GetViewports()[/font][/size]
[size=3][color=blue][font=Consolas]    For[/font][/color][font=Consolas] [color=blue]Each[/color] myVPortID [color=blue]As[/color] [color=#2b91af]ObjectId[/color] [color=blue]In[/color] myVPIDs[/font][/size]
[size=3][color=blue][font=Consolas]      Dim[/font][/color][font=Consolas] myVP [color=blue]As[/color] [color=#2b91af]Viewport[/color] = [color=blue]DirectCast[/color](myTrans.GetObject(myVPortID, [color=#2b91af]OpenMode[/color].ForWrite), [color=#2b91af]Viewport[/color])[/font][/size]
[size=3][font=Consolas]      DocumentManager.MdiActiveDocument.Editor.WriteMessage(myVP.Layer + vbNewLine)[/font][/size]
[size=3][font=Consolas]      VpLayerOff([color=#a31515]"test"[/color], myVPortID)[/font][/size]
[size=3][font=Consolas]      myVP.UpdateDisplay()[/font][/size]
[size=3][color=blue][font=Consolas]    Next[/font][/color][/size]
[size=3][color=blue][font=Consolas]  End[/font][/color][font=Consolas] [color=blue]If[/color][/font][/size]
[size=3][color=blue][font=Consolas]Next[/font][/color][/size]
[size=3][color=blue][font=Consolas]End[/font][/color][font=Consolas] [color=blue]Using[/color][/font][/size]
[size=3][color=blue][font=Consolas]End[/font][/color][font=Consolas] [color=blue]Sub[/color][/font][/size]

[size=3][color=blue][font=Consolas]Sub[/font][/color][font=Consolas] VpLayerOff([color=blue]ByVal[/color] myVpLyrName [color=blue]As[/color] [color=blue]String[/color], [color=blue]ByRef[/color] myVPID [color=blue]As[/color] [color=#2b91af]ObjectId[/color])[/font][/size]
[size=3][color=blue][font=Consolas]Dim[/font][/color][font=Consolas] I [color=blue]As[/color] [color=blue]Integer[/color][/font][/size]
[size=3][color=blue][font=Consolas]Dim[/font][/color][font=Consolas] Counter [color=blue]As[/color] [color=blue]Integer[/color][/font][/size]
[size=3][color=blue][font=Consolas]Dim[/font][/color][font=Consolas] myResBuff [color=blue]As[/color] [color=#2b91af]ResultBuffer[/color] = GetXData(myVPID, [color=#a31515]"ACAD"[/color])[/font][/size]
[size=3][color=blue][font=Consolas]Dim[/font][/color][font=Consolas] myNewResBuff [color=blue]As[/color] [color=blue]New[/color] [color=#2b91af]ResultBuffer[/color][/font][/size]
[size=3][color=blue][font=Consolas]If[/font][/color][font=Consolas] myResBuff [color=blue]Is[/color] [color=blue]Nothing[/color] = [color=blue]False[/color] [color=blue]Then[/color][/font][/size]
[size=3][color=blue][font=Consolas]For[/font][/color][font=Consolas] I = LBound(myResBuff.AsArray) [color=blue]To[/color] UBound(myResBuff.AsArray)[/font][/size]
[size=3][color=green][font=Consolas]  'debug print out Xdata[/font][/color][/size]
[size=3][font=Consolas]  DocumentManager.MdiActiveDocument.Editor.WriteMessage(myResBuff.AsArray(I).TypeCode & vbTab & myResBuff.AsArray(I).Value.ToString & vbNewLine)[/font][/size]
[size=3][color=blue][font=Consolas]  If[/font][/color][font=Consolas] myResBuff.AsArray(I).TypeCode = 1003 [color=blue]Then[/color][/font][/size]
[size=3][font=Consolas]    Counter = I + 1[/font][/size]
[size=3][color=blue][font=Consolas]    If[/font][/color][font=Consolas] myResBuff.AsArray(I).Value.ToString = myVpLyrName [color=blue]Then[/color][/font][/size]
[size=3][color=blue][font=Consolas]      Exit Sub[/font][/color][/size]
[size=3][color=blue][font=Consolas]    End[/font][/color][font=Consolas] [color=blue]If[/color][/font][/size]
[size=3][color=blue][font=Consolas]  End[/font][/color][font=Consolas] [color=blue]If[/color][/font][/size]
[size=3][color=blue][font=Consolas]Next[/font][/color][/size]
[size=3][color=blue][font=Consolas]Else[/font][/color][/size]
[size=3][font=Consolas]MsgBox([color=#a31515]"NO XData under that AppName."[/color])[/font][/size]
[size=3][color=blue][font=Consolas]End[/font][/color][font=Consolas] [color=blue]If[/color][/font][/size]

[size=3][color=blue][font=Consolas]If[/font][/color][font=Consolas] Counter = 0 [color=blue]Then[/color][/font][/size]
[size=3][color=blue][font=Consolas]For[/font][/color][font=Consolas] I = LBound(myResBuff.AsArray) [color=blue]To[/color] UBound(myResBuff.AsArray)[/font][/size]
[size=3][color=blue][font=Consolas]  If[/font][/color][font=Consolas] myResBuff.AsArray(I).TypeCode = 1002 [color=blue]Then[/color][/font][/size]
[size=3][font=Consolas]    Counter = I - 1[/font][/size]
[size=3][color=blue][font=Consolas]  End[/font][/color][font=Consolas] [color=blue]If[/color][/font][/size]
[size=3][color=blue][font=Consolas]Next[/font][/color][/size]
[size=3][color=blue][font=Consolas]End[/font][/color][font=Consolas] [color=blue]If[/color][/font][/size]
[size=3][color=green][font=Consolas]'adds existing data to the new result buffer we are creating[/font][/color][/size]
[size=3][color=blue][font=Consolas]For[/font][/color][font=Consolas] I = LBound(myResBuff.AsArray) [color=blue]To[/color] Counter - 1[/font][/size]
[size=3][font=Consolas]myNewResBuff.Add([color=blue]New[/color] [color=#2b91af]TypedValue[/color](myResBuff.AsArray(I).TypeCode, myResBuff.AsArray(I).Value))[/font][/size]
[size=3][color=blue][font=Consolas]Next[/font][/color][/size]
[size=3][color=green][font=Consolas]'appends the new frozen layer and closes the set[/font][/color][/size]
[size=3][font=Consolas]myNewResBuff.Add([color=blue]New[/color] [color=#2b91af]TypedValue[/color](1003, myVpLyrName))[/font][/size]
[size=3][font=Consolas]myNewResBuff.Add([color=blue]New[/color] [color=#2b91af]TypedValue[/color](1002, [color=#a31515]"}"[/color]))[/font][/size]
[size=3][font=Consolas]myNewResBuff.Add([color=blue]New[/color] [color=#2b91af]TypedValue[/color](1002, [color=#a31515]"}"[/color]))[/font][/size]
[size=3][color=green][font=Consolas]'debug display to see new record buffer[/font][/color][/size]
[size=3][font=Consolas]DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbNewLine & vbNewLine)[/font][/size]
[size=3][color=blue][font=Consolas]For[/font][/color][font=Consolas] I = LBound(myNewResBuff.AsArray) [color=blue]To[/color] UBound(myNewResBuff.AsArray)[/font][/size]
[size=3][color=green][font=Consolas]'debug print out Xdata[/font][/color][/size]
[size=3][font=Consolas]DocumentManager.MdiActiveDocument.Editor.WriteMessage(myNewResBuff.AsArray(I).TypeCode & vbTab & myNewResBuff.AsArray(I).Value.ToString & vbNewLine)[/font][/size]
[size=3][color=blue][font=Consolas]Next[/font][/color][/size]

[size=3][font=Consolas]AddXData(myVPID, myNewResBuff)[/font][/size]

[size=3][color=blue][font=Consolas]End[/font][/color][font=Consolas] [color=blue]Sub[/color][/font][/size]

[size=3][color=blue][font=Consolas]End Class[/font][/color][/size]
[/font]

Posted

As promised here are the Jerry Winters Functions that goes into the Class

 

[color=green][font=Consolas]'From Jerry Winters AU 2009 Class - "Store it in the DWG: XData,[/font][/color]
[color=green][font=Consolas]'Extension Dictionaries and Object Data Through .NET[/font][/color]
[color=blue][font=Consolas]Function[/font][/color][font=Consolas] GetXData([color=blue]ByVal[/color] EntityID [color=blue]As[/color] [color=#2b91af]ObjectId[/color], [color=blue]ByVal[/color] AppName [color=blue]As[/color] [color=blue]String[/color]) [color=blue]As[/color] [color=#2b91af]ResultBuffer[/color][/font]
[color=blue][font=Consolas]  Using[/font][/color][font=Consolas] myTrans [color=blue]As[/color] [color=#2b91af]Transaction[/color] = EntityID.Database.TransactionManager.StartTransaction[/font]
[color=blue][font=Consolas]  Dim[/font][/color][font=Consolas] selEnt [color=blue]As[/color] [color=#2b91af]Entity[/color] = EntityID.GetObject([color=#2b91af]OpenMode[/color].ForRead)[/font]
[color=blue][font=Consolas]  Dim[/font][/color][font=Consolas] myResBuffer [color=blue]As[/color] [color=#2b91af]ResultBuffer[/color] = selEnt.GetXDataForApplication(AppName)[/font]
[color=blue][font=Consolas]  Return[/font][/color][font=Consolas] myResBuffer[/font]
[color=blue][font=Consolas]  End[/font][/color][font=Consolas] [color=blue]Using[/color][/font]
[color=blue][font=Consolas]End[/font][/color][font=Consolas] [color=blue]Function[/color][/font]
[color=blue][font=Consolas] [/font][/color]
[color=green][font=Consolas]'From Jerry Winters AU 2009 Class - "Store it in the DWG: XData,[/font][/color]
[color=green][font=Consolas]'Extension Dictionaries and Object Data Through .NET[/font][/color]
[color=blue][font=Consolas]Sub[/font][/color][font=Consolas] AddXData([color=blue]ByVal[/color] EntityID [color=blue]As[/color] [color=#2b91af]ObjectId[/color], [color=blue]ByVal[/color] BufferIn [color=blue]As[/color] [color=#2b91af]ResultBuffer[/color])[/font]
[font=Consolas]  AddXDataApp(BufferIn.AsArray(0).Value)[/font]
[color=blue][font=Consolas]  Using[/font][/color][font=Consolas] myTrans [color=blue]As[/color] [color=#2b91af]Transaction[/color] = EntityID.Database.TransactionManager.StartTransaction[/font]
[color=blue][font=Consolas]    Dim[/font][/color][font=Consolas] selEnt [color=blue]As[/color] [color=#2b91af]Entity[/color] = EntityID.GetObject([color=#2b91af]OpenMode[/color].ForWrite)[/font]
[font=Consolas]    selEnt.XData = BufferIn[/font]
[font=Consolas]    myTrans.Commit()[/font]
[color=blue][font=Consolas]  End[/font][/color][font=Consolas] [color=blue]Using[/color][/font]
[color=blue][font=Consolas]End[/font][/color][font=Consolas] [color=blue]Sub[/color][/font]
[color=blue][font=Consolas] [/font][/color]
[color=green][font=Consolas]'From Jerry Winters AU 2009 Class - "Store it in the DWG: XData,[/font][/color]
[color=green][font=Consolas]'Extension Dictionaries and Object Data Through .NET[/font][/color]
[color=blue][font=Consolas]Sub[/font][/color][font=Consolas] AddXDataApp([color=blue]ByVal[/color] AppName [color=blue]As[/color] [color=blue]String[/color])[/font]
[color=blue][font=Consolas]  Dim[/font][/color][font=Consolas] myDWG [color=blue]As[/color] [color=#2b91af]Document[/color] = DocumentManager.MdiActiveDocument[/font]
[color=blue][font=Consolas]  Using[/font][/color][font=Consolas] myTrans [color=blue]As[/color] [color=#2b91af]Transaction[/color] = myDWG.TransactionManager.StartTransaction[/font]
[color=green][font=Consolas]    'Add the XData ApplicationName[/font][/color]
[color=blue][font=Consolas]    Dim[/font][/color][font=Consolas] myAppTable [color=blue]As[/color] [color=#2b91af]RegAppTable[/color] = myDWG.Database.RegAppTableId.GetObject([color=#2b91af]OpenMode[/color].ForWrite)[/font]
[color=blue][font=Consolas]    If[/font][/color][font=Consolas] myAppTable.Has(AppName) = [color=blue]False[/color] [color=blue]Then[/color][/font]
[color=blue][font=Consolas]      Dim[/font][/color][font=Consolas] myAppTableRecord [color=blue]As[/color] [color=blue]New[/color] [color=#2b91af]RegAppTableRecord[/color][/font]
[font=Consolas]        myAppTableRecord.Name = AppName[/font]
[font=Consolas]        myAppTable.Add(myAppTableRecord)[/font]
[font=Consolas]        myTrans.AddNewlyCreatedDBObject(myAppTableRecord, [color=blue]True[/color])[/font]
[color=blue][font=Consolas]    End[/font][/color][font=Consolas] [color=blue]If[/color][/font]
[font=Consolas]    myTrans.Commit()[/font]
[color=blue][font=Consolas]  End[/font][/color][font=Consolas] [color=blue]Using[/color][/font]
[color=blue][font=Consolas]End[/font][/color][font=Consolas] [color=blue]Sub[/color][/font]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...