Autodesk Official latest. NET tutorial (v) (vb.net edition)

Source: Internet
Author: User
Tags definition command line commit finally block keyword list
Tutorials   5th User interoperability: Hints and selection background hints usually contain a descriptive message, accompanied by a stop to let the user understand the given information and enter the data. Data can be entered in a variety of ways, such as through the command line, dialog box, or AutoCAD edit window. Given the hint to follow a certain format, the format should be consistent with the general AutoCAD hint, this is very important. For example, a keyword is delimited with a "/" number and placed in brackets [], and the default value is placed in <>. For an AutoCAD user, adherence to the uniform format will reduce the error of information understanding. When the user selects an entity in the AutoCAD command line, the entity is selected using the selection mechanism. This mechanism includes a hint that lets the user know what to choose and how to choose (such as a window or a single entity), and then a pause. Take a look at the prompts, such as the Pine command, and pedit to see if a single entity or multi-line option is available. Practice prompts: Tip: In this chapter, we'll prompt for an employee's name, position, salary, and department to create an employee block Index object. If the input department does not exist, we will prompt for the name of the department manager to create a new department. Before we go on, let's try to reuse the previous code. To make a choice, we will prompt the user to select or select an entity in a single window, and we show only the employee object in the select Set. In the previous chapters, we created an employee named "Earnest Shackleton", whose name was stored as Mtext in the "Employeeblock" Block definition (block table record). If we insert this block more than once, all we see is the name of the same employee. How can we customize this block so that each time we insert this block, the names of different employees are displayed? This will take the function of block properties. Properties are the text stored in each block index instance and are displayed as part of the instance. Property inherits the associated property from the property definition stored in the Block table record. Properties: Let's change the Mtext entity type to a property definition. In the Createemployeedefinition () function, replace the following code with    ' text: Dim text as Mtext = New mtext () text. Contents = "Earnest Shackleton" text. Location = center  defines Dim text as Attributedefinition = New attributedefinition (center, NoName, "Name:") for   ' attributes", Enter Name, Db. Textstyle) text. ColorIndex = 2  try to test the createemployeedefinition () function:     <commandmethod ("test" using the test command) ) > _    public Function Test ()         createemployeedefinition ()     End function  You should now be able to insert the Employeeblock block using the Insert command and determine an employee name for each instance. When you insert the employee block, notice where the block is inserted. Is it just being placed at the selected points or some offset? Try how to fix it. (Hint: Check the center of the block definition) modify Createemployee () to reuse  1) Let's Modify the Createemployee () function so that it can receive the name, salary, department, and position and return the Objectid of the employee block index that was created. The form of the function is as follows (you can change the order of the parameters)  public function Createemployee (ByVal name as String, ByVal division as String, ByVal salary as Dou BLE, ByVal pos as Point3D)       remove the Commandmethod attribute "CREATE" in the above function, This is no longer a command to create an employee. 3)                   Modify the code of the function so that you can correctly set the name, position, department, and salary of the Block index and its extended dictionary.
    • Replace
Dim br As New Blockreference (new Point3D (0), createemployeedefinition ()) for Dim br As New Blockreference (POS, creat Eemployeedefinition ())
    • Replace
Xrec.data = New Resultbuffer (_new typedvalue (Dxfcode.text, "Earnest Shackleton"), _new Typedvalue (DxfCode.Real, 72000), _new Typedvalue (Dxfcode.text, "Sales")) is Xrec.data = New Resultbuffer (_new typedvalue (dxfcode.text, name), _new TypedVal UE (Dxfcode.real, salary), _new Typedvalue (dxfcode.text, Division)) 4) because we replace the name of the employee with the attribute definition of the block, so we want to create a The corresponding property index to display the employee's name. Property indexes will use properties defined by the property.
    • Replace:
 btr. Appendentity (BR) ' joins the index into the model space trans. Addnewlycreateddbobject (BR, True) ' Let transactions know   for        Dim Attref as Attributereference = New attributereference ()   ' Traverse employee block to find attribute definition Dim empbtr as Blocktablerecord = trans. GetObject (BT ("Employeeblock"), Openmode.forread)  dim ID as objectid for each ID in empbtr    Dim ent as Entity = trans. GetObject (ID, openmode.forread, False) ' Open the current Object!     If TypeOf ent is attributedefinition Then '         ' Set the property to the property definition in the property index       Dim attdef as Attributedefinition = CType (ent, attributedefinition)       Attref.setpropertiesfrom (attdef)       attref.position = New Point3D (attdef.position.x + br. Position.x, _                                   ATTDEF.POSITION.Y + br. POSITION.Y, _                                   Attdef.position.z + Br. POSITION.Z)                       attref.height = attdef.height                     attref.rotation = attdef.rotation                     Attref.tag = attdef.tag                     attref.textstring = name       end ifnext btr. Appendentity (BR) ' Add index to model space   ' Add attribute index to block index BR. Attributecollection.appendattributE (attref)   ' let transaction processing know trans. Addnewlycreateddbobject (Attref, True) trans. Addnewlycreateddbobject (BR, True)    study the above code to see how to copy attributes from the attribute definition to the property index, except for the display text string. property is added to the property collection of the Block index. That's how you can customize employee names for each instance. 5 do not forget to return the Objectid of the employee block index, but only after the transaction is committed:  trans.commit () return BR. objectid 6)        test Createemployee. Adding a test command to test Createemployee: <commandmethod ("Test") > _ public Function Test ()          Createemployee ("Earnest Shackleton", "Sales", 10000, New Point3D (0)) End function    Modify Createdivision () for reuse: Let's Modify the Createdivision () function so that it receives the name of the department, the manager's name, and returns the Objectid of the department manager's extended record that you created. If the department manager already exists, the manager's name is not changed. 1)                     If you have previously called createdivision () in createemployeedefinition (), please comment it out because we do not need to create a department here 2)      Change the form of createdivision () so that it receives the name of the department and manager and returns a Objectid.  public Function createdivision (ByVal division as StrinG, ByVal Manager as String)      Modify the code of the above function to create the name and manager of the Department:
    • Replace:
Divdict = trans. GetObject (Acmedict.getat ("Sales"), Openmode.forwrite) is: divdict = trans. GetObject (Acmedict.getat (division), Openmode.forwrite)
    • Replace:
Acmedict.setat ("Sales", Divdict) is: Acmedict.setat (Division, Divdict)
    • Replace:
 mgrxrec.data = new Resultbuffer (new Typedvalue (Dxfcode.text, Randolph P. Brokwell))      Mgrxrec.data = new Resultbuffer (new Typedvalue (Dxfcode.text, manager))    don't forget to return to the department manager the objectid of this extended record, It is not returned until the transaction is committed.  trans.commit () ' Return to Department manager the Objectidreturn Mgrxrec.objectid of this extended record now comments out the Createdivision function that is invoked in the createemployeedefinition. 4)     now test the call createdivision function by using the test command. Use the Arxdbg tool to check whether the entry has been added to the named Object Dictionary under "Acme_division."  createdivision ("Sales", "Randolph P. Brokwell")   Use the Create command to build an employee:  we'll add a new command named Create. This command prompts for the employee's detailed information to create an employee block index. Let's take a look at how this command is used. 1       Let's add a new command named Create and declare a few commonly used variables and a try-finally block.      <commandmethod ("CREATE") > _    public Sub createemployee ()          dim db = hostapplicationservices.workingdatabase         Dim ed as Editor = Application.documentmanager.mdiactivedocument.editor        Dim trans as Transaction = db. Transactionmanager.starttransaction ()         try             Trans. Commit ()         finally             Trans. Dispose ()         end try     end sub 2)        let's define constants that you can use as a hint of default values for employees. Note that the Boolean value gotposition is used to determine whether the user has entered a position. Employee Name              -type:string          -The default value "Earnest Shackleton". Employee's Department name-type: String            -Default "Sales". Salary                    -Type: Double (non-negative and not zero)     -default value 10000. Job                   - Type: Point3D           -Default (0,0,0)   Add these constants to the back of the try statement:             Dim EmpName as New String ("Earnest Shackleton")              Dim Divname as New String ("Sales")              Dim Salary as New Double (): salary = 10000    & nbsp;       Dim position as New Point3D (0, 0, 0)              ' Boolean value to determine whether the user has entered a position              Dim gotposition as New Boolean (): Gotposition = False &NBSP;3)       Now let us prompt the user to enter a value. We first use the Promptxxxoptions class to initialize the prompt string to display.   ' Prompt to enter details for each employee Dim prname As Promptstringoptions = New promptstringoptions ("Enter Employee Name <" & EmpName & Amp ">" Dim prdiv as Promptstringoptions = New promptstringoptions ("Enter Employee Division <" & Divname & "> ") Dim prsal As Promptdoubleoptions = New promptdoubleoptions (" Enter Employee Salary < "& Salary &" > ") Dim pr Pos as Promptpointoptions = New promptpointoptions ("Enter Employee Position or")   note that the hint string uses angle brackets to display the value of the variable. This is the AutoCAD used to prompt the user for this value as the default value. 4)   When prompted to enter a position, we also provide a keyword list options, such as name, department and salary. If the user wants to change to a different value when selecting a point, he can select that keyword. An example of a command prompt is as follows: Command:createenter employee Position or [name/division/salary]:  to create an employee, the user selects a point while the other values are set to the default value. If the user wants to change other values, such as the name, he can enter "N" or full name "name" and enter the name: Command:createenter Employee Position or [name/division/salary]:nenter Employee name <earnest shackleton>:  If the user wants to select the default name again, he can press ENTER. Let's create a keyword list for job tips:  ' Add keywords for job tipsPRPOS.KEYWORDS.ADD ("Name") PrPos.Keywords.Add ("division") PrPos.Keywords.Add ("Salary")   ' Set the constraint condition of the hint Prpos.allownone = False ' do not allow no value  5 '       Now let's declare the Promptxxxresult variable to get the result of the hint:   ' Prompt Resultsdim prnameres as Promptresultdim prdivres as Promptresultdim prsalres as Promptdoubleresultdim PrPos Res as Promptpointresult 6)       the loop does not end until the user successfully enters a point. If the input is wrong, we will prompt the user and exit the function: To determine whether the user entered the keyword, we check the status of the Promptresult to conduct a:  ' loop to obtain the employee's detailed information. When the position is entered, the loop terminates. while (not (gotposition))            ' prompts for position           prposres = ed. GetPoint (prpos)            ' Get a point           if prposres.status = Promptstatus.ok then              gotposition = true              Position = Prposres.value         elseif prposres.status = Promptstatus.keyword Then ' Get a keyword               ' entered the name keyword               If prposres.stringresult = "Name" then                    ' Get Employee name                    prname.allowspaces = True                   Prnameres = ed. GetString (Prname)                    If prnameres.status <> Promptstatus.ok then                       return    & NBsp;             End if                   ' If get employee name succeeds                   If prnameres.stringresult <> "" Then                       EmpName = prnameres.stringresult                   End if              End If           else             ' error occurred while getting position               ed. Writemessage ("***error in Getting a", exiting!! "+ vbCrLf)              return             End If ' If you get a point end while 7       The above code prompts for a name only, add the code that prompts you for the salary and department. &NBSP;8)       When prompted to enter, we will use the obtained value to create the employee.              ' Create employee             Createemployee (EmpName, divname, salary, position)  9)        Now check to see if the department manager already exists. We do this by checking the name of the manager in the extended record of the department in Nod. If an empty string is checked, the user is prompted to enter the manager's name. Note that by modifying the createdivision () function, it is easier to get the manager's name.    Dim Manager as String = New string ("")    ' Create department   ' pass an empty string to the manager to check if it already exists    Dim Depmgrx Rec as xrecord   Dim xrecid as objectid   xrecid = Createdivision (divname, manager)      ' Open Department manager extension record     Depmgrxrec = trans. GetObject (Xrecid, Openmode.forread)      Dim Val as typedvalue    for each Val in depmgrxrec.data       Dim str as string       str = Val. value       If str = "then       ' manager has not been set, now set it          ' first prompt for manager's name         ed. Writemessage (vbCrLf)         Dim prmanagername as Promptstringoptions = New Promptstringoptions ("No Manager set for the" division! Enter Manager Name ")         prmanagername.allowspaces = true         Dim prmanagernameres as Promptresult = ed. GetString (prmanagername)         If prmanagernameres.status <> Promptstatus.ok then             Return         End if         ' Set manager's name &NBSP;&NBSP;&NBsp;     depmgrxrec.data = new Resultbuffer (new Typedvalue (Dxfcode.text, Prmanagernameres.stringresult))        end if    next 10)   To test the Create command   Select Set: Now let's create a command that displays the employee's details when the user selects an Employee object in the graph. We will use the Listemployee () function created in the previous chapter to export the employee details on the command line. Here are the steps you must follow:
    1. Invoke the "listemployees" command
    1. Call the editor getselection () function to select the entity
Dim res as Promptselectionresult = ed. GetSelection (Opts, filter)
    1. The filter above is used to filter the block index in the selection set. You can create the following filter list:
Dim fillist () as Typedvalue = {new Typedvalue (Dxfcode.start, INSERT)}dim filter as Selectionfilter = new Selectionfilter (fillist)
    1. To get an Objectid array from a SELECT set:
      ' If you choose to fail, do nothing       if not res. Status = Promptstatus.ok Then return      Dim SS as Autodesk.AutoCAD.EditorInput.SelectionSet = Res. value   Dim IDArray as ObjectId () = SS. Getobjectids ()   5. Finally, each objectid in the selection set is entered into the Listemployee () function to obtain an array of strings for the employee details. Output the employee's details to the command line. For example,:    ' gets all employees in the saemployeelist array              for each employeeId in idarray                 Listemployee (employeeId, saemployeelist)                   ' Output the employee details to the command line                  Dim Employeedetail as string                 for each employeedetail in saemployeelist                     ed. Writemessage (Employeedetail)                  next                                ed. Writemessage ("----------------------" + vbCrLf)              next 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.