/// <summary>
        /// Gets the window handle of the active process.
        /// </summary>
        /// <param name="processName">The name of the process to get the handle of.</param>
        /// <returns>The window handle of the active process.</returns>
        public IWin32Window GetWindowHandle(string processName)
        {
            using (var executionBlock = monitoredExecutionContext
                                        .MonitorMethod <WindowTools>(nameof(GetWindowHandle))
                                        .WithTiming())
            {
                try
                {
                    var processes = Process.GetProcessesByName(processName);

                    if (processes.Length > 0)
                    {
                        var h = processes[0].MainWindowHandle;
                        return(new WindowHandle(h));
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (Exception exception)
                {
                    executionBlock.LogException(exception);
                    return(null);
                }
            }
        }
예제 #2
0
 public IList <Family> GetFamilies(Document document)
 {
     using (var executionBlock = monitoredExecutionContext
                                 .MonitorMethod <FamilyHelper>(nameof(GetFamilies))
                                 .WithParameter(nameof(document), document)
                                 .WithTiming())
     {
         try
         {
             return(new FilteredElementCollector(document)
                    .WherePasses(new ElementClassFilter(typeof(Family)))
                    .Cast <Family>()
                    .ToList());
         }
         catch (Exception exception)
         {
             executionBlock.LogException(exception);
             return(null);
         }
     }
 }
예제 #3
0
 /// <summary>
 /// Place a <paramref name="familySymbol"/> in <paramref name="uiDocument"/>.
 /// </summary>
 /// <param name="uiDocument">Specifies the document to place the family symbol in.</param>
 /// <param name="familySymbol">Specifies the symbol that must be placed.</param>
 public void PlaceFamilyInstance(UIDocument uiDocument, FamilySymbol familySymbol)
 {
     using (var executionBlock = monitoredExecutionContext
                                 .MonitorMethod <ElementHelper>(nameof(PlaceFamilyInstance))
                                 .WithParameter(nameof(uiDocument), uiDocument)
                                 .WithParameter(nameof(familySymbol), familySymbol)
                                 .WithTiming())
     {
         try
         {
             uiDocument.PromptForFamilyInstancePlacement(familySymbol);
         }
         catch (Autodesk.Revit.Exceptions.OperationCanceledException)
         {
         }
         catch (Exception exception)
         {
             executionBlock.LogException(exception);
             throw;
         }
     }
 }
예제 #4
0
        public CADHostResult InsertCadContent(string contentPath, string name, IEnumerable <CADProperty> properties, bool placeInstance, CADMetadata cadMetadata)
        {
            using (var monitoredExecutionBlock = monitoredExecutionContext
                                                 .MonitorMethod <RevitHost>(nameof(InsertCadContent))
                                                 .WithParameter(nameof(contentPath), contentPath)
                                                 .WithParameter(nameof(name), name)
                                                 .WithParameter(nameof(properties), properties)
                                                 .WithParameter(nameof(placeInstance), placeInstance)
                                                 .WithTiming())
            {
                var parameters = new Dictionary <string, ParameterInfo>();
                try
                {
                    parameters = ConvertProperties(properties);
                }
                catch
                {
                    MessageBox.Show(Properties.Resources.MessageBoxCadPropertiesIncomplete_Text, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(new CADHostResult {
                        State = CADHostResultState.Failed
                    });
                }

                ElementType loadedElementType;
                using (var transaction = new Transaction(ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument.Document, "Insert Family"))
                {
                    transaction.Start();

                    var typeDataList = new List <TypeData>()
                    {
                        new TypeData()
                        {
                            Path       = contentPath,
                            Parameters = new Dictionary <string, ParameterInfo>(),
                            TypeName   = name
                        }
                    };

                    loadedElementType = familyHelper.LoadTypes(ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument.Document, typeDataList);

                    SetDefaultParameterValues(loadedElementType, properties, cadMetadata);

                    foreach (var parameter in parameters)
                    {
                        var revitParameter = parameterHelper.GetParameterBySearchString(loadedElementType, parameter.Value.Name);

                        if (revitParameter == null)
                        {
                            revitParameter = parameterHelper.GetParameterBySearchString(loadedElementType, parameter.Value.Name.Replace("_0_", "_"));
                        }

                        parameterHelper.SetParameterValue(revitParameter, parameter.Value.Value);
                    }

                    transaction.Commit();
                }

                if (!loadedElementType.IsValidObject)
                {
                    return(new CADHostResult {
                        State = CADHostResultState.Failed
                    });
                }

                UOLAddInUtilities.UpdatePropertiesWithValuesFromFamily(loadedElementType, properties, false);

                if (placeInstance)
                {
                    try
                    {
                        if ((FamilySymbol)loadedElementType != null && ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument.ActiveView.ViewType != ViewType.Elevation && ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument.ActiveView.ViewType != ViewType.Section)
                        {
                            new ElementHelper().PlaceFamilyInstance(ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument, (FamilySymbol)loadedElementType);
                        }
                        else
                        {
                            MessageBox.Show(Properties.Resources.MessageBoxCadContentLoadedCantPlace_Text, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    catch (Autodesk.Revit.Exceptions.InvalidOperationException invalidOperationException)
                    {
                        monitoredExecutionBlock.LogException(invalidOperationException);
                        MessageBox.Show(Properties.Resources.MessageBoxCadContentLoadedCantPlace_Text, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(new CADHostResult {
                            CADObject = loadedElementType, State = CADHostResultState.Succeeded
                        });
                    }
                }

                return(new CADHostResult {
                    CADObject = loadedElementType, State = CADHostResultState.Succeeded
                });
            }
        }