/// <summary>
 /// Copy values from another InsertInstanceOptions object
 /// </summary>
 /// <param name="source"></param>
 public void CopyFrom(InsertInstanceOptions source)
 {
     Id = source.Id;
       BlockDescription = source.BlockDescription;
       BlockId = source.BlockId;
       BlockName = source.BlockName;
       DocumentId = source.DocumentId;
       InsertSourceArchive = source.InsertSourceArchive;
       SourceArchive = source.SourceArchive;
       Url = source.Url;
       UrlDescription = source.UrlDescription;
       NeedToDisplayOptionsForm = source.NeedToDisplayOptionsForm;
       UpdateType = source.UpdateType;
       LayerStyle = source.LayerStyle;
       SkipNestedLinkedDefinitions = source.SkipNestedLinkedDefinitions;
 }
 /// <summary>
 /// Copy consturctor
 /// </summary>
 /// <param name="source"></param>
 public InsertInstanceOptions(InsertInstanceOptions source)
 {
     CopyFrom(source);
 }
        /// <summary>
        /// Initialize the combo box text field and description label text based on the current InstanceDefinition
        /// </summary>
        /// <param name="item"></param>
        void InitializeFromInsertInstance(InsertInstanceOptions item)
        {
            url = Rhino.UI.LOC.STR("URL: ");
              int start = url.Length;
              int end = start;

              InstanceDefinition idef = null == item ? null : item.InstanceDefinition;

              if (item == null)
              {
            linkDescription = string.Empty;
            blockDescription = string.Empty;
              }
              else if (null != idef)
              {
            url += string.IsNullOrWhiteSpace(idef.UrlDescription) ? idef.Url : idef.UrlDescription;
            blockDescription = idef.Description;
            end = url.Length;
              }
              else
              {
            blockDescription = item.BlockDescription;
              }

              // TODO:
              // linkLabel.LinkArea = new LinkArea(start, end - start);

              UpdateDescriptionText(item);

              SetPreviewImage(item);
        }
 /// <summary>
 /// Helper used when sorting lists of Instance Options
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <returns></returns>
 public static int Compare(InsertInstanceOptions a, InsertInstanceOptions b)
 {
     if (a == null && b == null)
     return 0;
       if (a == null)
     return -1;
       if (b == null)
     return 1;
       return string.Compare(a.BlockName, b.BlockName);
 }
        string FormatPathForDisplay(InsertInstanceOptions instance)
        {
            if (null == instance)
            return string.Empty;

              string path = instance.SourceArchive;

              if (string.IsNullOrWhiteSpace(path))
            return string.Empty;

              string result = path;

              if (!string.IsNullOrWhiteSpace(ModelPath) && Path.IsPathRooted(path))
              { // If using absolute paths and active model is named and path is a relative path
            result = PathAbsoluteFromRelativeTo(path, true, ModelPath, true);
              }

              return (result ?? string.Empty);
        }
 /// <summary>
 /// Update the description text label based on current CommandArgs values
 /// </summary>
 void UpdateDescriptionText(InsertInstanceOptions instance)
 {
     blockDescriptionIsReadOnly = (InsertAs == InsertAs.Block || null == instance || !instance.NeedToDisplayOptionsForm);
       // Catch formatting exceptions on Asian language systems.
       try
       {
     if (DroppingFileOnRhino)
     {
       linkDescription = string.Format(Rhino.UI.LOC.STR("Dropping file \"{0}\" onto Rhino"), FormatPathForDisplay(CommandOptions));
     }
     else if (null == instance || string.IsNullOrWhiteSpace(instance.BlockName))
     {
       linkDescription = string.Empty;
     }
     else
     {
       // Update the description text to include the link style and SourceArchive if included
       if (instance.NeedToDisplayOptionsForm && InsertAs == InsertAs.Block)
     linkDescription = string.Format(Rhino.UI.LOC.STR("Creating block from file \"{0}\""), FormatPathForDisplay(instance));
       else if (instance.NeedToDisplayOptionsForm)
     linkDescription = string.Format(Rhino.UI.LOC.STR("Inserting file \"{0}\""), FormatPathForDisplay(instance));
       else if (instance.UpdateType == InstanceDefinitionUpdateType.Linked)
     linkDescription = string.Format(Rhino.UI.LOC.STR("Linked to file \"{0}\""), FormatPathForDisplay(instance));
       else if (instance.UpdateType == InstanceDefinitionUpdateType.LinkedAndEmbedded)
     linkDescription = string.Format(Rhino.UI.LOC.STR("Embedded and linked to file \"{0}\""), FormatPathForDisplay(instance));
       else
     linkDescription = Rhino.UI.LOC.STR("Embedded block");
     }
       }
       catch (Exception exception)
       {
     linkDescription = string.Empty;
     Rhino.Runtime.HostUtils.ExceptionReport(exception);
       }
 }
        /// <summary>
        /// Update CommandArgs
        /// </summary>
        /// <param name="failSilently"></param>
        /// <param name="instanceOptions"></param>
        /// <returns>Returns true if everything is okay or false if some condition existed that required the user to confirm some
        /// value prior to closing and they chose not to</returns>
        bool UpdateCommandArgs(bool failSilently, ref InsertInstanceOptions instanceOptions)
        {
            if (null == instanceOptions)
              {
            if (!failSilently)
              InvalidValueMessageBox(Rhino.UI.LOC.STR("You must select a block or file to insert"));
            return false;
              }
              // If inserting as a block and it is a new block which is going to be created from an external file and the InsertFileOptionsForm
              // has never been displayed for this item then display the form
              if (InsertAs == InsertAs.Block && instanceOptions.InsertSourceArchive && instanceOptions.NeedToDisplayOptionsForm)
              {
            var temp = ShowOptionsForm(instanceOptions.SourceArchive, instanceOptions.BlockDescription, instanceOptions.UrlDescription, instanceOptions.Url, true);
            if (null == temp)
              return false;
            // Copy the settings from the options form to the specified item
            instanceOptions.CopyFrom(temp);
            instanceOptions.DocumentId = DocumentId;
              }
              // Set the insert as value
              CommandOptions.InsertAs = InsertAs;
              // Update the insertion point values based on the current dialog settings
              CommandOptions.PromptForInsertionPoint = promptInsertionPoint;
              CommandOptions.InsertionPoint = InsertionPoint;
              // Update the scale setting flags based on the current dialog values
              CommandOptions.PromptForScale = promptScale;
              CommandOptions.UniformlyScale = uniformlyScale;
              // Update rotation settings based on current dialog values
              CommandOptions.PromptForRotationAngle = promptRotation;
              CommandOptions.RotationAngle = RhinoMath.ToRadians(rotation);
              //
              // Validate scale, must be non-zero value
              //
              // Will only validate controls that are enabled
              var xScale = enableScaleXControls ? scaleX : 1.0;
              var yScale = enableScaleYZControls ? scaleY : 1.0;
              var zScale = enableScaleYZControls ? scaleZ : 1.0;
              if (uniformlyScale)
            CommandOptions.Scale = new Point3d(xScale, xScale, xScale);
              else
            CommandOptions.Scale = new Point3d(xScale, yScale, zScale);
              // Make sure you are not attempting to insert this model into itself
              if (InsertAs == InsertAs.Block && instanceOptions.InsertSourceArchive && 0 == string.Compare(instanceOptions.SourceArchive, DocumentPath, StringComparison.InvariantCultureIgnoreCase))
              {
            InvalidValueMessageBox(Rhino.UI.LOC.STR("You can not insert a model into itself"));
            return false;
              }

              return true;
        }
 /// <summary>
 /// Initialize the block preview image control
 /// </summary>
 /// <param name="item"></param>
 /// <summary>
 /// Initialize the block preview image control
 /// </summary>
 /// <param name="item"></param>
 void SetPreviewImage(InsertInstanceOptions item)
 {
     #if ON_OS_WINDOWS
       if (null == item)
       {
     PreviewBitmap = null;
     return;
       }
       var cursor = null == Window ? null : Window.Cursor;
       if (null != Window) Window.Cursor = System.Windows.Input.Cursors.Wait;
       PreviewBitmap = item.GetPreviewBitmap(PreviewProjection, PreviewDisplayMode, new System.Drawing.Size(200, 120));
       if (null != Window) Window.Cursor = cursor;
     #endif
 }
 private void OnFileButtonClicked(string fileName)
 {
     // File dialog canceled
       if (string.IsNullOrWhiteSpace(fileName))
     return;
       if (!File.Exists(fileName))
       {
     InvalidValueMessageBox(string.Format(Rhino.UI.LOC.STR("File \"{0}\" not found"), fileName));
     return;
       }
       // Make sure you are not attempting to insert this model into itself
       if (InsertAs == ViewModels.InsertAs.Block && string.Compare(ModelPath, fileName, StringComparison.InvariantCultureIgnoreCase) == 0)
       {
     InvalidValueMessageBox(Rhino.UI.LOC.STR("You can not insert a model into itself"));
     return;
       }
       // Not inserting as a block so just cook up a name from the file name and add it to the list
       var item = new InsertInstanceOptions(CommandOptions);
       // Default block name is file name without extension
       var blockName = Path.GetFileNameWithoutExtension(fileName);
       // If there is already a block with this name cook up a decorated name
       if (null != Document && null != Document.InstanceDefinitions.Find(blockName, true))
     blockName = Document.InstanceDefinitions.GetUnusedInstanceDefinitionName(blockName);
       // Default insertion command arguments
       var defaults = InsertInstanceOptions.Defaults;
       // Set the new items properties using the default insert values
       item.FromFileName(fileName, blockName, defaults.UpdateType, defaults.LayerStyle, defaults.SkipNestedLinkedDefinitions);
       // Set the flag that will require the options form to be displayed when this dialog is closing if this item is selected
       item.NeedToDisplayOptionsForm = true;
       // Add the new item to the combo box
       _blockList.Add(item);
       RaisePropertyChanged(() => blockList);
       RaisePropertyChanged(() => blockStringList);
       // Select the newly added item
       blockListSelectedIndex = _blockList.Count - 1;
 }