macro disappear

macro disappear

Anonymous
Not applicable
1,729 Views
13 Replies
Message 1 of 14

macro disappear

Anonymous
Not applicable

im tryin to create pipe by using the code below, i built the solution wit no errors but te macro disappear !! i dont know why,, can anyone elp me please

 

public Pipe CreateNewPipe(Document document, ElementId systemTypeId, ElementId levelId)
{
    // find a pipe type
            
    FilteredElementCollector collector = new FilteredElementCollector(document);
    collector.OfClass(typeof(PipeType));
    PipeType pipeType = collector.FirstElement() as PipeType;
            
    Pipe pipe = null;
    if (null != pipeType)
    {
        // create pipe between 2 points
        XYZ p1 = new XYZ(0, 0, 0);
        XYZ p2 = new XYZ(10, 0, 0);

        pipe = Pipe.Create(document, systemTypeId, pipeType.Id, levelId, p1, p2);
    }

    return pipe;
}
0 Likes
Accepted solutions (1)
1,730 Views
13 Replies
Replies (13)
Message 2 of 14

FAIR59
Advisor
Advisor

a macro has this form:

 

        public void myMacro()
        {
        }

 

returntype void, no parameters.

 

using your method, the macro could be something like this:

 

		public void CreatePipe()
		{
			Document doc = this.ActiveUIDocument.Document;
			List<Level> levels = new FilteredElementCollector(doc)
				.OfClass(typeof(Level))
				.Cast<Level>()
				.ToList();
			ElementId levelId = levels.FirstOrDefault().Id;
			List<PipingSystemType> pipesystems = new FilteredElementCollector(doc)
				.OfClass(typeof(PipingSystemType))
				.Cast<PipingSystemType>()
				.ToList();
			if (pipesystems.Count==0) return;
			ElementId systemtype = pipesystems.FirstOrDefault().Id;
			Pipe newPipe = CreateNewPipe(doc,systemtype ,levelId);
			this.ActiveUIDocument.Selection.SetElementIds(new List<ElementId>() {newPipe.Id});
	}

 

 

Note: if you want to change the document, you have to use a TransAction.

0 Likes
Message 3 of 14

Anonymous
Not applicable

Dear Fair59 thank you for your kind response 

 

im learning revit API using the link below but after finishing the code i build it and it show no errors but the macro disappear from the module 

 

http://www.revitapidocs.com/2015/9550265f-5760-3c28-d023-d0373285855b.htm

 

i tried your code but it gives me three errors:

 

'a.ThisDocument' does not contain a definition for 'ActiveUIDocument' and no extension method 'ActiveUIDocument' accepting a first argument of type 'a.ThisDocument' could be found (are you missing a using directive or an assembly reference?) (CS1061) - C:\Users\AHMAD\AppData\Local\Temp\{6A873E7C-98CD-47FB-B322-36D28FFAB07B}\Revit\DocHookups4428\1157782784\a\Source\a\ThisDocument.cs:42,24

 

The name 'CreateNewPipe' does not exist in the current context (CS0103) - C:\Users\AHMAD\AppData\Local\Temp\{6A873E7C-98CD-47FB-B322-36D28FFAB07B}\Revit\DocHookups4428\1157782784\a\Source\a\ThisDocument.cs:54,19

 

'a.ThisDocument' does not contain a definition for 'ActiveUIDocument' and no extension method 'ActiveUIDocument' accepting a first argument of type 'a.ThisDocument' could be found (are you missing a using directive or an assembly reference?) (CS1061) - C:\Users\AHMAD\AppData\Local\Temp\{6A873E7C-98CD-47FB-B322-36D28FFAB07B}\Revit\DocHookups4428\1157782784\a\Source\a\ThisDocument.cs:55,9

0 Likes
Message 4 of 14

FAIR59
Advisor
Advisor

I don't use macro's often, but I think this should be the structure of the module.

public partial class ThisApplication
	{
		private void Module_Startup(object sender, EventArgs e)
		{

		}

		private void Module_Shutdown(object sender, EventArgs e)
		{

		}

		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
		}
		#endregion

		public void CreatePipe()
		{
		}

		public Pipe CreateNewPipe(Document document,ElementId systemTypeId, ElementId levelId)
		{
    			// find a pipe type
    			return pipe;
		}
	}

As you are starting to program, you should search the net for more information on getting started with Macro's. The first site I found is:

 

http://thebuildingcoder.typepad.com/blog/2015/10/rtc-classes-and-getting-started-with-revit-macros.h...

0 Likes
Message 5 of 14

FAIR59
Advisor
Advisor

I see from the errors that you are working on a Document-macro. My previous replies are based on a Application-macro.

 

in a document-macro

code for the document:  Document doc= this.Document;

code for setting the selection: this.Selection.SetElementIds(new List<ElementId>() {newPipe.Id});

Message 6 of 14

Anonymous
Not applicable

thanks FAIR59

 

i built the code and it gives me no errors and the macro didnt disappear, i ran the macro but nothing happen on revit,below is the code

 

/*
 * Created by SharpDevelop.
 * User: ashonnar
 * Date: 6/19/2017
 * Time: 12:18 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.DB.Macros;


namespace a
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("1C724706-132D-46B2-8A14-C7715606378B")]
    public partial class ThisDocument
    {
        private void Module_Startup(object sender, EventArgs e)
        {

        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion
        public void Createnewpipe()
        {
        }
        public static Pipe Createnewpipe(Document document, ElementId systemTypeId,  ElementId levelId )
{
    // find a pipe type
            
    FilteredElementCollector collector = new FilteredElementCollector(document);
    collector.OfClass(typeof(PipeType));
    PipeType pipeType = collector.FirstElement() as PipeType;
            
    Pipe pipe = null;
    if (null != pipeType)
    {
        // create pipe between 2 points
        XYZ p1 = new XYZ(10000);
        XYZ p2 = new XYZ(100001000);

        pipe = Pipe.Create(document, systemTypeId, pipeType.Id, levelId, p1, p2);
        
    }

    return pipe;
}
    }
}

 

0 Likes
Message 7 of 14

so-chong
Advocate
Advocate

Hi, try do something like this ( i have comment some code of yours, because they don't work in a macro environment. Secondly always create a transaction):

 

 

        public void Createnewpipe()
        
       // public static Pipe Createnewpipe(Document document, ElementId systemTypeId,  ElementId levelId )
        {

            Document document = this.ActiveUIDocument.Document;
            UIDocument uidoc = this.ActiveUIDocument; 

            ViewPlan view = document.ActiveView as ViewPlan;    
            Level level = view.GenLevel;
            
            // find a pipe system type
            FilteredElementCollector sysCollector = new FilteredElementCollector(document);
            sysCollector.OfClass(typeof(PipingSystemType));
            ElementId pipeSysTypeId = sysCollector.FirstElementId();
               
            // find a pipe type
            FilteredElementCollector collector = new FilteredElementCollector(document);
            collector.OfClass(typeof(PipeType));
            PipeType pipeType = collector.FirstElement() as PipeType;
                    
            Pipe pipe = null;
            if (null != pipeType)
            {
                // create pipe between 2 points
                XYZ p1 = new XYZ(10000);
                XYZ p2 = new XYZ(100001000);
        
                using (Transaction t = new Transaction(document, "Create Pipe"))
                {
                    t.Start();
                    pipe = Pipe.Create(document, pipeSysTypeId, pipeType.Id, level.Id, p1, p2);
                    t.Commit();
                }
            }
        
         //   return pipe;
        }

0 Likes
Message 8 of 14

FAIR59
Advisor
Advisor

I deliberately gave you code, without any codelines, so you could see the structure of a macro.

 

Now that you haven't any errors anymore, you can start coding the macro. The entry point for the macro is the method

public void Createnewpipe().

At the moment this method is empty, so the macro does nothing.

 

 

0 Likes
Message 9 of 14

Anonymous
Not applicable

i dont know what are the problems, i tried all what you said and below is the upper part of the code, maybe something wrong in it, thank you

 

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.DB.Macros;
using Autodesk.Revit.UI.Events;
using Createnewpipe;
using Autodesk.Revit.UI.Plumbing;

 

 

0 Likes
Message 10 of 14

so-chong
Advocate
Advocate

Hi, try delete this using directive (using Createnewpipe) and rebuild your code in the SharpDevelop editor.

 

 

revitapiforum.png

0 Likes
Message 11 of 14

so-chong
Advocate
Advocate
Accepted solution
/*
 * Created by SharpDevelop.
 * User: S.Li
 * Date: 22-6-2017
 * Time: 9:19
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Mechanical;

namespace testPipe
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("78D0C737-4539-4CA3-B88B-078BFD2773C1")]
	public partial class ThisApplication
	{
		private void Module_Startup(object sender, EventArgs e)
		{

		}

		private void Module_Shutdown(object sender, EventArgs e)
		{

		}

		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
		}
		#endregion
		public void Createnewpipe()
		{            
		    Document document = this.ActiveUIDocument.Document;
                    UIDocument uidoc = this.ActiveUIDocument; 

                    ViewPlan view = document.ActiveView as ViewPlan;    
                    Level level = view.GenLevel;
            
                    // find a pipe system type
                    FilteredElementCollector sysCollector = new FilteredElementCollector(document);
                    sysCollector.OfClass(typeof(PipingSystemType));
                    ElementId pipeSysTypeId = sysCollector.FirstElementId();
               
                    // find a pipe type
                    FilteredElementCollector collector = new FilteredElementCollector(document);
                    collector.OfClass(typeof(PipeType));
                    PipeType pipeType = collector.FirstElement() as PipeType;
                    
                    Pipe pipe = null;
                    if (null != pipeType)
                    {
                         // create pipe between 2 points
                         XYZ p1 = new XYZ(100, 0, 0);
                         XYZ p2 = new XYZ(1000, 0, 1000);
        
                         using (Transaction t = new Transaction(document, "Create Pipe"))
                         {
                             t.Start();
                             pipe = Pipe.Create(document, pipeSysTypeId, pipeType.Id, level.Id, p1, p2);
                             t.Commit();
                         }
                    }
		}
	}
}
0 Likes
Message 12 of 14

Anonymous
Not applicable

thank you very much faris

0 Likes
Message 13 of 14

Anonymous
Not applicable

thank you very much sochong

0 Likes
Message 14 of 14

Anonymous
Not applicable

what is the differences between public class and public void ?

0 Likes